Click here to Skip to main content
15,923,164 members
Home / Discussions / C#
   

C#

 
GeneralRe: key_down event fired twice Pin
igalep1326-May-10 11:36
igalep1326-May-10 11:36 
AnswerRe: key_down event fired twice Pin
Luc Pattyn6-May-10 11:37
sitebuilderLuc Pattyn6-May-10 11:37 
GeneralRe: key_down event fired twice Pin
igalep1326-May-10 11:47
igalep1326-May-10 11:47 
GeneralRe: key_down event fired twice Pin
Luc Pattyn6-May-10 11:55
sitebuilderLuc Pattyn6-May-10 11:55 
GeneralRe: key_down event fired twice Pin
igalep1326-May-10 12:09
igalep1326-May-10 12:09 
QuestionFtpWebRequest - (UploadFile) every other character is NULL Pin
Alaric_6-May-10 10:30
professionalAlaric_6-May-10 10:30 
AnswerRe: FtpWebRequest - (UploadFile) every other character is NULL Pin
Alaric_6-May-10 10:34
professionalAlaric_6-May-10 10:34 
QuestionIssue With Multiple Events Handled By One Delegate ... Pin
Mrestivo6-May-10 6:11
Mrestivo6-May-10 6:11 
I am trying to figure out how I can properly support multiple (& identical) events using a single delegate/handler. What I am experiencing is that I can easily tied my events to a single handler (i.e., using += ), but when any one of the events fire each instance of the handler method executes. I did not expect this behaviour as I had assumed that each delegate creation was instantiating a specific copy of itself and would be internally resolved. I have a full project space that shows this behaviour. Here is some code from the main program that shows the overall structure that I currently have:

namespace EventsDelegateIssue
{
public partial class MainForm : Form
{

#region - Variables -
DoSomeWork[] oDoSomeWork;

delegate void SetMessageCallback(string Message);
#endregion

#region - Constructor/Destructor -
public MainForm()
{
InitializeComponent();

//Create some worker objects and connect their events to handlers
oDoSomeWork = new DoSomeWork[3];
for (int i = 0; i < oDoSomeWork.Length; i++)
{
oDoSomeWork[i] = new DoSomeWork();
oDoSomeWork[i].Index = i;
oDoSomeWork[i].OnMessage += new PostMessageEvent.OnMessageEvent(MainForm_OnMessage);
onMessage_event += new onMessage_delegate(onMessage_method);
oDoSomeWork[i].OnData += new RaiseDataEvent.OnRaiseDataEvent(MainForm_OnData);
onData_event += new onData_delegate(onData_method);
}

}
#endregion

#region - Form Control Event Handler(s) -
private void btnDoSomeWork_Click(object sender, EventArgs e)
{
//Let's start our worker objects such that [0] finishes first, [2] second, and [1] third
oDoSomeWork[0].Count(10);
oDoSomeWork[1].Count(50);
oDoSomeWork[2].Count(20);
}
#endregion

#region - DoSomeWork Event Handlers -
//These worker event handlers check to make sure that they can handle the task on the proper thread

//Handle worker generated messages
public delegate void onMessage_delegate(PostMessageEvent messageHandler);
private event onMessage_delegate onMessage_event;
private void MainForm_OnMessage(PostMessageEvent messageHandler)
{
if (this.InvokeRequired == true)
{ BeginInvoke(onMessage_event, new object[] { messageHandler }); }
else
{ onMessage_method(messageHandler); }
}
private void onMessage_method(PostMessageEvent messageHandler)
{ UpdateMessage(messageHandler.strMessage); }


//Handle worker generated data
public delegate void onData_delegate(RaiseDataEvent dataEvent);
private event onData_delegate onData_event;
private void MainForm_OnData(RaiseDataEvent dataEvent)
{
if (this.InvokeRequired == true)
{ BeginInvoke(onData_event, new object[] { dataEvent }); }
else
{ onData_method(dataEvent); }
}
private void onData_method(RaiseDataEvent dataEvent)
{
UpdateMessage(dataEvent.Tag.ToString() + "; " + dataEvent.Data);
}
#endregion

#region - Update Message Function -
private void UpdateMessage(string Message)
{
//This message handler also checks to see if it is operating on the correct thread so as to
//not throw an exception
if (this.txtMessages.InvokeRequired)
{
SetMessageCallback d = new SetMessageCallback(UpdateMessage);
this.Invoke(d, new object[] { Message });
}
else
{ txtMessages.AppendText(DateTime.Now.ToString("HH:mm:ss") + "=> " + Message + "\r\n"); }
}
#endregion
}
}
AnswerRe: Issue With Multiple Events Handled By One Delegate ... Pin
Not Active6-May-10 7:00
mentorNot Active6-May-10 7:00 
GeneralRe: Issue With Multiple Events Handled By One Delegate ... Pin
Mrestivo6-May-10 7:56
Mrestivo6-May-10 7:56 
GeneralRe: Issue With Multiple Events Handled By One Delegate ... Pin
Not Active6-May-10 8:21
mentorNot Active6-May-10 8:21 
Questionvariable type class Pin
tek 20096-May-10 5:24
tek 20096-May-10 5:24 
AnswerRe: variable type class Pin
Henry Minute6-May-10 5:40
Henry Minute6-May-10 5:40 
AnswerRe: variable type class Pin
harold aptroot6-May-10 5:44
harold aptroot6-May-10 5:44 
GeneralRe: variable type class Pin
tek 20096-May-10 5:50
tek 20096-May-10 5:50 
QuestionAuthentication Token USB Pin
Maurinho06-May-10 3:40
Maurinho06-May-10 3:40 
AnswerRe: Authentication Token USB Pin
taztazon6-May-10 3:45
taztazon6-May-10 3:45 
GeneralRe: Authentication Token USB Pin
Maurinho06-May-10 3:59
Maurinho06-May-10 3:59 
AnswerRe: Authentication Token USB Pin
MumbleB6-May-10 4:03
MumbleB6-May-10 4:03 
GeneralRe: Authentication Token USB Pin
Maurinho06-May-10 4:47
Maurinho06-May-10 4:47 
AnswerRe: Authentication Token USB Pin
PIEBALDconsult6-May-10 4:33
mvePIEBALDconsult6-May-10 4:33 
GeneralRe: Authentication Token USB Pin
taztazon6-May-10 4:44
taztazon6-May-10 4:44 
AnswerRe: Authentication Token USB Pin
T M Gray6-May-10 11:27
T M Gray6-May-10 11:27 
QuestionShow Progress Bar % with estimated time while uploading a file Pin
attalurisubbu6-May-10 3:38
attalurisubbu6-May-10 3:38 
AnswerRe: Show Progress Bar % with estimated time while uploading a file Pin
Dave Kreskowiak6-May-10 4:24
mveDave Kreskowiak6-May-10 4:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.