473,761 Members | 2,455 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

BeginInvoke calls EventHandler with different parameters than supplied

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(o bject sender, System.EventArg s e)
{
Thread t = new Thread(new ThreadStart(OnT hreadStart));
t.Start();
}
private void OnThreadStart()
{
UpdateTextBox(n ull, new MyEventArgs(Dat eTime.Now.ToLon gTimeString())) ;
}
private void UpdateTextBox(o bject sender, EventArgs e)
{
if(InvokeRequir ed)
{
// When using MyEventHandler everything works as expected.
//MyEventHandler h = new MyEventHandler( UpdateTextBox);
EventHandler h = new EventHandler(Up dateTextBox);
BeginInvoke(h, new object[] {sender, e});
}
else
{
// This cast will fail !!!!!!!!!!!!!!! !!!!!!!
MyEventArgs args = (MyEventArgs)e;
textBox1.Text = string.Format(" Current time: {0}", args.CurrentTim e);
}
}

public class MyEventArgs : EventArgs
{
public readonly string CurrentTime;

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


Jul 21 '05 #1
2 2693
Sandor Heese <sa*********@ho tmail.com> 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 - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #2
Yes, I agree it is bizarre. (probably a performance optimalization)

Thanks Jon,

Sandor
"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
Sandor Heese <sa*********@ho tmail.com> 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 - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Jul 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
6633
by: Grandma Wilkerson | last post by:
My question concerns the documentation for Control.BeginInvoke(). At one point is says: "Executes the specified delegate asynchronously with the specified arguments, on the thread that the control's underlying handle was created on." later in that same documentation page it says... "Note The BeginInvoke method calls the specified delegate back on a
0
1423
by: Jedrzej Miadowicz | last post by:
I have a problem when using ISynchronizeInvoke.BeginInvoke with polymorphic parameters. It seems that if I try to call BeginInvoke and in the array of parameters place an object that's derived from a base class my method expects as a parameter, the method gets only an object of the type of the base class, rather than my full object. I know this sounds convoluted, so here's an example. My program consists of an engine and a WindowsForms...
3
8883
by: David Logan | last post by:
I have an application using sockets, and it uses the asynchronous method for receiving (and others, but receiving is the basic problem.) In short, I want: class someClass: Form { mySocketClass sss; ...
3
14105
by: Koji Ishii | last post by:
I have a worker thread, and the thread needs to listen to WinForms Control's SizeChanged event. Here's my code. Control c = ...; if (c.InvokeRequired) { c.BeginInvoke(===don't know how to write here===); } else { c.SizeChanged += new EventHandler(this.OnOwnerSizeChanged); }
2
255
by: Sandor Heese | last post by:
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...
9
5667
by: john doe | last post by:
I have a question about BeginInvoke method found on the Control class. To quote the docs: "Executes the specified delegate asynchronously with the specified arguments, on the thread that the control's underlying handle was created on." Which is fine, but I'm wondering how does this method get called asynchronously if it's on the same thread we are working on? Surely it blocks the thread until returned?
5
2485
by: GT | last post by:
Could someone please explain the difference of (refer to example code below) d.BeginInvoke("some text", null,null); //Alternative A and BeginInvoke( d, new object { "some text" } //Alternative B Both calls causes asynchonous execution to begin at the address specified by the delegate, and both calls returns an IAsyncResult, isn't that so? However I've experienced that these calls can cause different program behaviour....
2
3812
by: Flack | last post by:
Hello, If I understand BeginInvoke correctly, when it is called your delegate is run on a thread pool thread. Now, if you supplied a callback delegate, that too is called on the same thread pool thread. My question is this: Do I ever need to check the value of InvokeRequired in my callback method before working with some GUI controls? Won't it always be required since the callback is running on a thread pool thread? I see some code...
2
4424
by: =?Utf-8?B?a2VubmV0aG1Abm9zcGFtLm5vc3BhbQ==?= | last post by:
vs2005, c# Trying to understand why one way works but the other doesnt. I have not tried to create a simpler mdi/child/showdialog app for posting purposes (I think even this would not be so small or simple). I am hoping the description will generate some ideas I can check out. PROBLEM: ----------------- Switching to UI thread using Invoke(), everything seems good.
0
10123
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9975
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9909
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9788
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8794
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7342
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6623
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5241
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5384
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.