Problem with delegate Syntax in C#

前端 未结 2 1757
粉色の甜心
粉色の甜心 2020-12-11 19:16

I built a Testbox to learn something about threading in windows form applications. Silverlight and Java are providing the Dispatcher, which really helps when updating GUI El

2条回答
  •  半阙折子戏
    2020-12-11 19:42

    In many cases like this, the simplest approach is to use a "captured variable" to pass state between the threads; this means you can keep the logic localised:

    public void UpdateTestBox(string newText)
    {
        BeginInvoke((MethodInvoker) delegate {
            tb_output.Text = newText;
        });        
    }
    

    The above is particularly useful if we expect it to be called on the worker thread (so little point checking InvokeRequired) - note that this is safe from either the UI or worker thread, and allows us to pass as much or as little state between the threads.

提交回复
热议问题