.NET: How to have background thread signal main thread data is available?

后端 未结 7 1959
终归单人心
终归单人心 2021-02-14 04:54

What is the proper technique to have ThreadA signal ThreadB of some event, without having ThreadB sit blocked waiting for an e

7条回答
  •  隐瞒了意图╮
    2021-02-14 05:15

    The BackgroundWorker class is answer in this case. It is the only threading construct that is able to asynchronously send messages to the thread that created the BackgroundWorker object. Internally BackgroundWorker uses the AsyncOperation class by calling the asyncOperation.Post() method.

    this.asyncOperation = AsyncOperationManager.CreateOperation(null);
    this.asyncOperation.Post(delegateMethod, arg);
    

    A few other classes in the .NET framework also use AsyncOperation:

    • BackgroundWorker
    • SoundPlayer.LoadAsync()
    • SmtpClient.SendAsync()
    • Ping.SendAsync()
    • WebClient.DownloadDataAsync()
    • WebClient.DownloadFile()
    • WebClient.DownloadFileAsync()
    • WebClient...
    • PictureBox.LoadAsync()

提交回复
热议问题