WPF System.InvalidOperationException: The calling thread cannot access this object because a different thread owns it

前端 未结 3 2007
滥情空心
滥情空心 2021-01-12 15:28

In my Windows I have a TextBox which I like to update (text property) from another thread. When doing so, I get the InvalidOperationException (see title). I have found diffe

3条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-12 16:04

    For WPF, I find this construct:

     BackgroundWorker bw = new BackgroundWorker();
      bw.DoWork += ( s, e ) =>
      {
      };
      bw.RunWorkerCompleted += ( s, e ) =>
      {
      };
      bw.RunWorkerAsync();
    

    to be the most useful. The RunWorkerCompleted block will typically update an ObservableCollection or fire off a RaisePropertyChangedEvent.

提交回复
热议问题