Multithreading Question
-
Hi all.I want to update my UI via a background thread. I have concluded in two options : 1.) Checking InvokeRequired private delegate void UpdateUIHandler(); ... protected virtual void UpdateUI1() { if(!InvokeRequired) { ... } else { this.Invoke(new UpdateUIHandler(UpdateUI1)); } } 2.) Using an anonymous method and MethodInvoker: ... public void SomeMethodRunningInDiffirentThread() { ... this.Invoke(new MethodInvoker( delegate() { UpdateUI2(); } )); ... } public void UpdateUI2(){...} So the question is the following.Which approach is more suitable?Are there any deficiencies or side-effects in any of these two methods (e.g performance,stupidity etc.) or any conceptual error in the approaches. Thanx in advance.
-
Hi all.I want to update my UI via a background thread. I have concluded in two options : 1.) Checking InvokeRequired private delegate void UpdateUIHandler(); ... protected virtual void UpdateUI1() { if(!InvokeRequired) { ... } else { this.Invoke(new UpdateUIHandler(UpdateUI1)); } } 2.) Using an anonymous method and MethodInvoker: ... public void SomeMethodRunningInDiffirentThread() { ... this.Invoke(new MethodInvoker( delegate() { UpdateUI2(); } )); ... } public void UpdateUI2(){...} So the question is the following.Which approach is more suitable?Are there any deficiencies or side-effects in any of these two methods (e.g performance,stupidity etc.) or any conceptual error in the approaches. Thanx in advance.
Well, the second way is more verbose, as you need two methods to do the job. You also left out the check for
InvokeRequired
in the second method. It's not a mistake, as Invoke internally checks if it's on the UI thread before doing the actual job. I'd say both are equivalent, but you'd want to prefer the first. Regards Senthil _____________________________ My Blog | My Articles | WinMacro -
Well, the second way is more verbose, as you need two methods to do the job. You also left out the check for
InvokeRequired
in the second method. It's not a mistake, as Invoke internally checks if it's on the UI thread before doing the actual job. I'd say both are equivalent, but you'd want to prefer the first. Regards Senthil _____________________________ My Blog | My Articles | WinMacroThanx for answering.In the second method i have intentionally left out InvokeRequired check.
-
Thanx for answering.In the second method i have intentionally left out InvokeRequired check.
You can also shorten up the second one a bit. It avoids the overhead of an extra object creation and delegate call. 2.) Using an anonymous method and MethodInvoker: ... public void SomeMethodRunningInDiffirentThread() { ... this.Invoke((MethodInvoker)delegate() { UpdateUI2(); } )); ... } Jared Parsons jaredp@beanseed.org http://spaces.msn.com/members/jaredp/