How expensive is Invoke?
-
I want to use Control.Invoke to interact between the UI thread and other threads. In some cases, I expect I might need to make thousands of Invoke calls in a short period of time. How expensive is Invoke? Might I be better off optimizing by grouping together data so I don't have to make many Invoke calls, or is it nothing to worry about?
-
I want to use Control.Invoke to interact between the UI thread and other threads. In some cases, I expect I might need to make thousands of Invoke calls in a short period of time. How expensive is Invoke? Might I be better off optimizing by grouping together data so I don't have to make many Invoke calls, or is it nothing to worry about?
Control.Invoke itself is not expensive. Using Reflection to invoke a function is. But you are not using reflection. So the answer is: Not expensive. (unless you want to send me money) ;P I rated this article 2 by mistake. It deserves more. I wanted to get to the second page... - vjedlicka 3:33 25 Nov '02
-
I want to use Control.Invoke to interact between the UI thread and other threads. In some cases, I expect I might need to make thousands of Invoke calls in a short period of time. How expensive is Invoke? Might I be better off optimizing by grouping together data so I don't have to make many Invoke calls, or is it nothing to worry about?
Adequate efficiency is not usually something that can be predicted, so you might just have to test things. However, even if you were not making cross-thread calls, thousands of calls in a short period of time (say milliseconds) is not as efficient as batching some data together. This would be especially true if each call only passes a small amount of data. Cross-thread calls will add to execution time, but may or may not be the crucial factor. If things aren't happening as fast as you'd like, it can't hurt to try batching some data together. Also, keep in mind that Invoke() is synchronous, so each call will wait for completion. BeginInvoke() might be worth looking at, since it is asynchrous, and will return immediately. However, it might be less attractive if the data needs to be synchronised. Which one to use really depends on the particular situation. Cheers