开发者

C# - How to get synchronous UI update from the InvokeRequired pattern?

I have a number of threads to append text to a rich text box. With Invoke(), I very easily get deadlock between the mai开发者_JS百科n UI thread and the worker threads. With BeginInvoke(), I get async UI update, which I do not want. I want the texts appear in the text box instantly, not at some later stage that I can't determine.

How do I do my task?

Thanks!


You cannot magically run on the UI thread without touching the UI thread.
If you want to call Invoke, you need to make the UI thread stop waiting for the background threads.

However, you should just call BeginInvoke instead.
BeginInvoke isn't any less instant than Invoke; the only difference is that Invoke forces the calling thread to wait until the UI thread has a chance to run the delegate.

You would call Invoke if the background thread needs to wait for the UI thread to run the delegate (eg, if it returns a value).
In normal cases, you should always call BeginInvoke; there's usually no point in making the background thread wait until the UI thread is free.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜