Is BeginInvoke / EndInvoke good practice to call in main thread?

Is it good to use a delegate call for the MainForm stream - this way ?:

Txt.MainForm.EndInvoke( Txt.MainForm.BeginInvoke( new MethodInvoker(delegate() { // code here } ))); 
+4
source share
3 answers

No - because if you call EndInvoke , it will be blocked until the delegation is complete. If you want this behavior, just use Invoke .

In other words, if you are trying to do something other than locking until your (delegate presumably UI-modified) executes in the user interface thread, you must explain what it is. If there is nothing, then Invoke will give you simpler code.

+8
source

This doesn't make much sense because the code starts an asynchronous call and then immediately waits for the call to complete. That is, you end up expecting on the calling thread.

+2
source

Apart from the others mentioned (I believe that the EndInvoke - BeginInvoke chain is just an example of using a delegate): using delegates is 100% normal. If this is the only use of the delegate body, there is no need to define it as a named method. This is cleaner in code, and there is no need to skip through the file. Consider using the new syntax for delegates:

 new MethodInvoker(() => { // code here }) 
0
source

All Articles