BackgroundWorker ShowDialog导致应用程序停止

时间:2012-08-13 20:21:04

标签: c# user-interface backgroundworker

我一直在尝试在我的应用程序中实现BackgroundWorker,到目前为止,它还没有顺利完成。在一个新的主题上,我想打开一个新的表单,它将有一个进度条和一个标签来报告进度,但是,这个效果不佳。当我调用ShowDialog时,应用程序不再响应。这是因为我的代码是从我的Form1运行的,我正在显示WorkingForm?此外,这可以实现更清洁吗?

private void button14_Click(object sender, EventArgs e)
{
    List<object> param = new List<object>();
    object[] objectparams = new object[1];
    objectparams[0] = null;
    Opera opera = new Opera();
    System.Reflection.MethodInfo clearOpera = opera.GetType().GetMethod("ClearOpera");
    param.Add(clearOpera);
    param.Add(opera);
    param.Add(objectparams);
    backgroundWorker1.RunWorkerAsync(param);
}

private void button2_Click_1(object sender, EventArgs e)
{
    Browser.cancelPending = true;
}
private delegate void getnewform();

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
    mainForm main = new mainForm();
    TestURLGUI4.Form1 form = (TestURLGUI4.Form1)Application.OpenForms[0];
    var variab = (bool)form.Invoke(new getnewform(main.AskForConfirmation));
        List<object> param = e.Argument as List<object>;

        List<object> result = new List<object>();
        var method = param[0] as MethodInfo;
        object[] parameters = param[2] as object[];
        if (parameters[0] == null)
        {
            result.Add(method.Invoke(param[1], null));
            result.Add(false);
        }
        else
        {
            result.Add(method.Invoke(param[1], parameters));
            if (parameters.Contains(true))
                result.Add(true);
        }
        int progress = (100 * Browser.progressValue) / Browser.progressMax;

        backgroundWorker1.ReportProgress(progress);

        // If the BackgroundWorker.CancellationPending property is true, cancel
        if (backgroundWorker1.CancellationPending)
        {
            Console.WriteLine("Cancelled");
            Browser.cancelPending = true;
        }
        e.Result = result;
}

private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
        TestURLGUI4.WorkingForm form = (TestURLGUI4.WorkingForm)Application.OpenForms[1];
        form.progressBar1.Value = e.ProgressPercentage;

        form.label1.Text = Browser.progressValue + "/" + Browser.progressMax;
        Application.DoEvents();
}

private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    List<object> param = e.Result as List<object>;
    if (e.Cancelled == false && param.Contains(true))
    {
        Display.DisplayURLs(param[0] as SortableBindingList<URL>);
        TestURLGUI4.WorkingForm form = (TestURLGUI4.WorkingForm)Application.OpenForms[1];
        MessageBox.Show("Done");

    }
    else if (e.Cancelled == false && param.Contains(false))
    {
        TestURLGUI4.WorkingForm form = (TestURLGUI4.WorkingForm)Application.OpenForms[1];
        MessageBox.Show("Done");
    }


}

    public class mainForm
{
public void AskForConfirmation()
{
    TestURLGUI4.Form1 form = (TestURLGUI4.Form1)Application.OpenForms[0];
    var workingForm = new TestURLGUI4.WorkingForm();
    workingForm.ShowDialog(form);
    workingForm.DialogResult = DialogResult.None;

}
}

编辑: 好的,我已根据建议更新了我的代码,现在,这会在System.Windows.Forms.dll中产生stackoverflowexception:

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
    mainForm main = new mainForm();
    TestURLGUI4.Form1 form = (TestURLGUI4.Form1)Application.OpenForms[0];
        List<object> param = e.Argument as List<object>;
        List<object> result = new List<object>();
        var method = param[0] as MethodInfo;
        object[] parameters = param[2] as object[];
        if (parameters[0] == null)
        {
            result.Add(method.Invoke(param[1], null));
            result.Add(false);
        }
        else
        {
            result.Add(method.Invoke(param[1], parameters));
            if (parameters.Contains(true))
                result.Add(true);
        }
        int progress = (100 * Browser.progressValue) / Browser.progressMax;

        backgroundWorker1.ReportProgress(progress);

        // If the BackgroundWorker.CancellationPending property is true, cancel
        if (backgroundWorker1.CancellationPending)
        {
            Console.WriteLine("Cancelled");
            Browser.cancelPending = true;
        }
        e.Result = result;


}

private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
    TestURLGUI4.Form1 form1 = (TestURLGUI4.Form1)Application.OpenForms[0];
    if (Application.OpenForms.Count >= 2)
    {
        TestURLGUI4.WorkingForm form2 = (TestURLGUI4.WorkingForm)Application.OpenForms[1];
        form2.progressBar1.Value = e.ProgressPercentage;

        form2.label1.Text = Browser.progressValue + "/" + Browser.progressMax;
        Application.DoEvents();
    }
    else if(Application.OpenForms.Count == 1)
    {
        var workingForm = new TestURLGUI4.WorkingForm();
        workingForm.ShowDialog(form1);
    }
}

2 个答案:

答案 0 :(得分:4)

BackgroundWorker的目的是调用另一个线程(而不是UI线程)上的代码。通过Invoke方法调用DoWork,您完全无视BackgroundWorker的目的。在启动工作人员之前,请完成所有UI工作。如果您需要在工作者工作时与用户进行交互,请在ProgressChanged处理程序中执行它 - 它在UI线程上运行,您不需要使用{{1在Invoke

通过在ProgressChanged中调用UI工作,您可能会遇到死锁的风险,这将导致程序挂起

答案 1 :(得分:0)

你无法在其他线程上运行UI。必须在主线上。

在启动新线程之前实例化UI。在新线程中,对要使用的控件使用交叉线程调用方法。在这里查看例如http://msdn.microsoft.com/en-us/library/ms171728.aspx