WPF C# - Editing a listbox from another thread

后端 未结 3 992
萌比男神i
萌比男神i 2020-12-29 13:32

I realize what I\'m doing is probably pretty silly, but I\'m in the middle of learning WPF and would like to know how to do this.

I have a window with a listbox on i

3条回答
  •  半阙折子戏
    2020-12-29 14:28

    You'll want to use Dispatcher.BeginInvoke. For example, if the listbox is named listbox

      // On worker thread
      listbox.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal,
      new Action(delegate() { listbox.Items.Add("Server started") });
    

    This will queue up the delegate to be executed on the UI thread that listbox belongs to.

提交回复
热议问题