繁体   English   中英

C#如何检查对话框是否仍然打开?

[英]C# How to check if dialog box is still open?

因此,当关闭事件发生时,我有一个后台工作者可能仍在工作。

我这样叫cancelasync():

void ProgressDialog_Closing(object sender, CancelEventArgs e)
{
  if (_worker.IsBusy)
  {
    //notifies the async thread that a cancellation has been requested.
    _worker.CancelAsync();
  }
}

但是,由于runworker_completed方法而导致崩溃,该方法指出在未打开对话框时无法设置对话框结果。 如何检查对话框是否仍然打开?

void _worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
      if (!Dispatcher.CheckAccess())
      {
        //run on UI thread
        RunWorkerCompletedEventHandler handler = _worker_RunWorkerCompleted;
        Dispatcher.Invoke(handler, null, DispatcherPriority.SystemIdle, new object[] { sender, e });
        return;
      }
      else
      {
        if (e.Error != null)
        {
          error = e.Error;
        }
        else if (!e.Cancelled)
        {
          //assign result if there was neither exception nor cancel
          result = e.Result;
        }
        ProgressBar.Value = ProgressBar.Maximum;
        CancelButton.IsEnabled = false;


        //set the dialog result, which closes the dialog

        if (error == null && !e.Cancelled)
        {
          DialogResult = true;
        }
        else
        {
          DialogResult = false;
        }

      }

    }

构造函数没什么特别的,runworkerthread是启动工作程序并设置委托运行的函数。

public bool RunWorkerThread(object argument, Func<object> workHandler)
    {
      //store reference to callback handler and launch worker thread
      workerCallback = workHandler;
      _worker.RunWorkerAsync(argument);


      return ShowDialog() ?? false;
    }
    DialogBox dialogBox = new DialogBox();

    // Show window modally 
    // NOTE: Returns only when window is closed
    Nullable<bool> dialogSelection = dialogBox.ShowDialog();
    if dialogSelection = null // box is still open

http://msdn.microsoft.com/en-us/library/system.windows.window.showdialog(v=vs.110).aspx

我认为直到用户做出选择或对话框关闭之前,dialogSelection都为null。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM