Invoke() execution

  • Thread starter Thread starter vooose
  • Start date Start date
V

vooose

Suppose you call

this.Invoke(new MyDelegate(MyMethod));
this.Invoke(new MyDelegate(MyMethod2));

(assume not calling from owning thread so marshall occurs)

Is MyMethod *guaranteed* to finish execution before MyMethod2 starts?

Regards
 
If the mydelegate was async the method then it would kick off a new thread
and return. It could then do the same for the second line. Splitting hairs
here but the mydelegate method completed starting a new thread in a
different VM's sync. The threads that actually are carrying out the work in
different VM's are not dependent on eachother. They could be on different
machines.

http://www.codeproject.com/csharp/delegate_bedtime.asp
 
Yahoo said:
If the mydelegate was async the method then it would kick off a new thread
and return. It could then do the same for the second line. Splitting hairs
here but the mydelegate method completed starting a new thread in a
different VM's sync. The threads that actually are carrying out the work in
different VM's are not dependent on eachother. They could be on different
machines.

Delegates themselves aren't synchronous or asynchronous - they're just
invoked in a synchronous or asynchronous way.
 
vooose said:
Suppose you call

this.Invoke(new MyDelegate(MyMethod));
this.Invoke(new MyDelegate(MyMethod2));

(assume not calling from owning thread so marshall occurs)

Is MyMethod *guaranteed* to finish execution before MyMethod2 starts?

Well, you haven't said what "this" is - if it's a control, for
instance, then yes, MyMethod will definitely finish before Invoke
returns.
 
Thanks for your replies. "this" is a Control - I should have clarified
that initially. What about if you replace Invoke with BeginInvoke? I
realise this executes it async but since MyMethod was marshalled before
MyMethod2 shouldnt it always finish first as well (ie MyMethod gets hold
of the underlying window thread first ALWAYS)

Regards
 
vooose said:
Thanks for your replies. "this" is a Control - I should have clarified
that initially. What about if you replace Invoke with BeginInvoke? I
realise this executes it async but since MyMethod was marshalled before
MyMethod2 shouldnt it always finish first as well (ie MyMethod gets hold
of the underlying window thread first ALWAYS)

Regards

I'm pretty sure it will using BeginInvoke - however I always feel a bit
uncomfortable about relying on this type of behavior. If you really want the
two methods to execute in Method1, Method2 order, use BeginInvoke on a
method that executes Method1 and Method2 synchronously. It will certainly
make youo intention clearer to anyone looking at the code.
Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk
 
It is posting to a fifo queue so not sure under what circumstances it would
not work in order posted.
 
Back
Top