0

我需要启用我的 gui 上的取消或停止按钮。但是当我运行事件时,我的 gui 会锁定。我明白为什么?

所以我开始一个新线程,运行循环,检查我是否点击了取消。在循环内部,我启动了一个新线程来处理该工作,然后更新 gui。

   //this is button click event.

    filesToImport = [list of obj, with information abount som task that needs to be done]
        TaskScheduler taskScheduler = TaskScheduler.FromCurrentSynchronizationContext(); // current gui context 

    // start a new thread
              Task.Factory.StartNew(() => {
                foreach (fics_ds_import job in filesToImport)
                {
    // have the user cancel();

                  if (this.CancellationTokenSource.IsCancellationRequested)
                  {
                    //this.CancellationTokenSource.Token.ThrowIfCancellationRequested();
                    break;
                  }

// start new thread for doing the task, and update the gui.
                  Task.Factory.StartNew(() => this.DoSomeWork(this.ConnectionStringTxt.Text, job), this.CancellationTokenSource.Token, TaskCreationOptions.None, taskScheduler).ContinueWith(o => this.UpdateProcessStatus(), taskScheduler);
                }
              }, this.CancellationTokenSource.Token, TaskCreationOptions.LongRunning, taskScheduler);
4

0 回答 0