Order of message posts through BeginInvoke preserved?

  • Thread starter Thread starter Jeff Newman
  • Start date Start date
J

Jeff Newman

Does anyone know if the order of delegate calls onto a thread through
BeginInvoke are guaranteed to come in the same order that they are
placed? For example, if the following calls happen in order:

control.BeginInvoke(new InvokeDelegate(functionOne));
control.BeginInvoke(new InvokeDelegate(functionTwo));

Is there now a guarantee that functionOne will be executed before
function two on the thread that created control? Does this hold if
the code above happens to be executing on the same thread that is
being invoked upon (ie control.InvokeRequired == false)?

This seems like an obvious question to me (that the order would be
preserved), but we're seeing some behavior that appears to indicate
otherwise and I'm trying to cover our bases.


Thanks!
 
I would suspect that the order is undefined for asynchronous operations.
Being async, the calls are likely marshalled to a separate thread for
execution. if multiple calls are made, then you have multiple threads, all
running at the same priority. In *most* cases they would get processed in
the order they were given to the scheduler, however there's nothing that
says that if the scheduler processes a higher priority thread (like a
driver) that when it comes back to the apps priority that it has to do them
in any order. It just round-robins all waiting threads at that priority.
You shouldn't make any assumption about the order that they will run - if
the order is important, you should enforce it through synchronization
objects.
 
Yes, but you're assuming that they *get* to the queue in the same order that
you call Invoke for the reasons I've outlined. I don't think you can
guarantee that - I sure wouldn't rely on it. Again, you should never make
any assumption about the order of asynchronous processing. If you need
there to be some rigid order, you should enforce it, not just hope that
things will somehow work out.


--

Chris Tacke, eMVP
Join the Embedded Developer Community
http://community.opennetcf.com
 
Back
Top