In C# what is the recommended way of passing data between 2 threads?

后端 未结 9 844
长发绾君心
长发绾君心 2020-12-08 01:36

I have my main GUI thread, and a second thread running inside it\'s own ApplicationContext (to keep it alive, even when there is no work to be done). I want to call a method

9条回答
  •  不思量自难忘°
    2020-12-08 01:47

    Use a synchronization object to signal the thread that it needs to process the new data (or the GUI's new state). One relatively simple way to do this is to use an event object. Here's a run-down of how that would work:

    1. GUI thread an 2nd thread share an event object (so they both know about it)
    2. 2nd thread typically runs in a loop of some sort, and each time it waits for the event to be signaled
    3. GUI thread signals the event when it needs the 2nd thread to do something
    4. When the 2nd thread is done, it resets the event and waits again (or exits)

提交回复
热议问题