Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is BeginInvoke/EndInvoke good practice for invoking on main thread?

Tags:

c#

delegates

Is it good practice to invoke delegate for MainForm thread - this way?:

Txt.MainForm.EndInvoke(
Txt.MainForm.BeginInvoke(
    new MethodInvoker(delegate() 
       { // code here }
)));
like image 959
Przemysław Michalski Avatar asked Dec 29 '22 06:12

Przemysław Michalski


2 Answers

No - because if you're calling EndInvoke, that will block until the delegate has completed. If you want that behaviour, just use Invoke instead.

To put it another way: if you're trying to do something other than blocking until your (presumably UI-modifying) delegate has executed in the UI thread, you should explain what that something is. If there isn't anything else, then Invoke will give you simpler code.

like image 130
Jon Skeet Avatar answered Feb 14 '23 01:02

Jon Skeet


It doesn't make a lot of sense as the code fires up an asynchronous call and then immediately waits for the call to finish. I.e. you end up waiting on the calling thread.

like image 22
Brian Rasmussen Avatar answered Feb 14 '23 01:02

Brian Rasmussen