PC Review


Reply
Thread Tools Rate Thread

BeginInvoke calls EventHandler with different parameters than supplied

 
 
Sandor Heese
Guest
Posts: n/a
 
      23rd Jun 2004
Question,

When using BeginInvoke (on a From to marshal to the UI thread) with the
EventHandler delegate I see some strange behaviour. The problem is that I
call the UpdateTextBox method with a class derived from EventArgs ( i.e.
MyEventArgs) and when the BeginInvoke is called and the UpdateTextBox
methode is call on the UI thread the parameter e (EventArgs) does not
contain the derived MyEventArgs object but a EventArgs object.

The strange thing is that if I create a delegate with the same signature as
the EventHandler ( MyEventHandler) everything works as expected. (See
comments in the code below)

Can anyone explane this to me?

Sandor

(Methods below are implemented on a Form derived class)

private void button1_Click(object sender, System.EventArgs e)
{
Thread t = new Thread(new ThreadStart(OnThreadStart));
t.Start();
}
private void OnThreadStart()
{
UpdateTextBox(null, new MyEventArgs(DateTime.Now.ToLongTimeString()));
}
private void UpdateTextBox(object sender, EventArgs e)
{
if(InvokeRequired)
{
// When using MyEventHandler everything works as expected.
//MyEventHandler h = new MyEventHandler(UpdateTextBox);
EventHandler h = new EventHandler(UpdateTextBox);
BeginInvoke(h, new object[] {sender, e});
}
else
{
// This cast will fail !!!!!!!!!!!!!!!!!!!!!!
MyEventArgs args = (MyEventArgs)e;
textBox1.Text = string.Format("Current time: {0}", args.CurrentTime);
}
}

public class MyEventArgs : EventArgs
{
public readonly string CurrentTime;

public MyEventArgs(string currentTime)
{
CurrentTime = currentTime;
}
}
delegate void MyEventHandler(object sender, EventArgs e);




 
Reply With Quote
 
 
 
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      23rd Jun 2004
Sandor Heese <(E-Mail Removed)> wrote:
> When using BeginInvoke (on a From to marshal to the UI thread) with the
> EventHandler delegate I see some strange behaviour. The problem is that I
> call the UpdateTextBox method with a class derived from EventArgs ( i.e.
> MyEventArgs) and when the BeginInvoke is called and the UpdateTextBox
> methode is call on the UI thread the parameter e (EventArgs) does not
> contain the derived MyEventArgs object but a EventArgs object.


Yes - for some reason, if you call Invoke or BeginInvoke with an
EventHandler delegate, whatever parameters you supply will be ignored.
Bizarre, no?

It's documented for Invoke, but not BeginInvoke. Here's the docs for
Invoke:

<quote>
The delegate can be an instance of EventHandler, in which case the
sender parameter will contain this control, and the event parameter
will contain EventArgs.Empty. The delegate can also be an instance of
MethodInvoker, or any other delegate that takes a void parameter list.
A call to an EventHandler or MethodInvoker delegate will be faster than
a call to another type of delegate.
</quote>

--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
Sandor Heese
Guest
Posts: n/a
 
      23rd Jun 2004
Yes, I agree it is bizarre. (probably a performance optimalization)

Thanks Jon,

Sandor


"Jon Skeet [C# MVP]" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Sandor Heese <(E-Mail Removed)> wrote:
> > When using BeginInvoke (on a From to marshal to the UI thread) with the
> > EventHandler delegate I see some strange behaviour. The problem is that

I
> > call the UpdateTextBox method with a class derived from EventArgs ( i.e.
> > MyEventArgs) and when the BeginInvoke is called and the UpdateTextBox
> > methode is call on the UI thread the parameter e (EventArgs) does not
> > contain the derived MyEventArgs object but a EventArgs object.

>
> Yes - for some reason, if you call Invoke or BeginInvoke with an
> EventHandler delegate, whatever parameters you supply will be ignored.
> Bizarre, no?
>
> It's documented for Invoke, but not BeginInvoke. Here's the docs for
> Invoke:
>
> <quote>
> The delegate can be an instance of EventHandler, in which case the
> sender parameter will contain this control, and the event parameter
> will contain EventArgs.Empty. The delegate can also be an instance of
> MethodInvoker, or any other delegate that takes a void parameter list.
> A call to an EventHandler or MethodInvoker delegate will be faster than
> a call to another type of delegate.
> </quote>
>
> --
> Jon Skeet - <(E-Mail Removed)>
> http://www.pobox.com/~skeet
> If replying to the group, please do not mail me too



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with various BeginInvoke() calls =?Utf-8?B?R1Q=?= Microsoft C# .NET 5 29th Aug 2006 01:49 PM
BeginInvoke calls EventHandler with different parameters than supplied Sandor Heese Microsoft Dot NET Framework Forms 1 23rd Jun 2004 09:38 PM
BeginInvoke calls EventHandler with different parameters than supplied Sandor Heese Microsoft C# .NET 1 23rd Jun 2004 09:38 PM
BeginInvoke calls EventHandler with different parameters than supplied Sandor Heese Microsoft Dot NET 2 23rd Jun 2004 09:32 PM
BeginInvoke calls disappears to God knows where Kallis Microsoft C# .NET 5 9th Jan 2004 05:38 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:04 AM.