来自BackgroundWorker的对话框等待用户输入

时间:2014-05-13 22:08:58

标签: c# wpf multithreading backgroundworker

我可以从BackgroundWorker打开Dialog并暂停直到用户决定吗?

MyWorker = new System.ComponentModel.BackgroundWorker();
MyWorker.DoWork += new System.ComponentModel.DoWorkEventHandler(CopyWorker_DoWork);
MyWorker.RunWorkerAsync();

private void MyWorker_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
{
    int i=0;
    while ((i) < collection.Count())
    {
       cListEntry item = collection[i];
       if (item.conflicted ==true) {
            Dispatcher.BeginInvoke(new Action(() =>
            {
                var dialog = new WindowConflict();
                //Open dialog and wait for user action
                if (dialog.ShowDialog() == true)
                {
                    item.level = dialog.Response;
                } 

1 个答案:

答案 0 :(得分:0)

你应该使用invoke而不是begininvoke,begininvoke允许任务继续运行,但你希望它等待。