C# Events between threads executed in their own thread (How to)?

后端 未结 4 1090
孤街浪徒
孤街浪徒 2020-12-30 02:27

I\'d like to have two Threads. Let\'s call them :

  • Thread A
  • Thread B

Thread A fires an event and thread B listen to this event. When th

4条回答
  •  抹茶落季
    2020-12-30 03:24

    The easiest way is probably to subscribe using an event handler which just marshal the "real" handler call onto thread B. For instance, the handler could call Control.BeginInvoke to do some work on thread B:

    MethodInvoker realAction = UpdateTextBox;
    foo.SomeEvent += (sender, args) => textBox.BeginInvoke(realAction);
    
    ...
    
    private void UpdateTextBox()
    {
        // Do your real work here
    }
    

提交回复
热议问题