表单在后台工作程序中不显示为Dialog? [英] Form does not displayed as Dialog inside backgroundworker?

查看:139
本文介绍了表单在后台工作程序中不显示为Dialog?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



这是我的代码:

  //从Form1 
private void backgroundWorkerResult_DoWork(object sender,DoWorkEventArgs e)
{
//在这里执行某些操作

backgroundWorkerResult.ReportProgress (100);

frmMessageBox frmMsgBox = new frmMessageBox();
frmMsgBox.ShowDialog();
}

即使我已经显示了 frmMsgBox 作为一个对话框我仍然可以点击 Form1 应该不是?如何解决这个问题?

解决方案

我创建了一个简单的代码示例,可以用来了解后台工作者的工作原理。将代码复制到测试表单并添加以下控件


  1. 标签控件 - 将其命名为'lblStatus'

  2. 进度条控制 - 应该命名progressBar1。

  3. 添加名为btnStartAsyncOperation和btnCancel的两个按钮和链接他们的点击事件
  4. 。 >

基本上,您应该在 RunWorkerCompleted 事件

中显示结果

  public partial class Form1:Form 
{
BackgroundWorker backgroundWorker;

public Form1()
{
InitializeComponent();
backgroundWorker = new BackgroundWorker {WorkerReportsProgress = true,WorkerSupportsCancellation = true};

backgroundWorker.DoWork + = new DoWorkEventHandler(BackgroundWorker_DoWork);
backgroundWorker.ProgressChanged + = new ProgressChangedEventHandler(BackgroundWorker_ProgressChanged);
backgroundWorker.RunWorkerCompleted + = new RunWorkerCompletedEventHandler(BackgroundWorker_RunWorkerCompleted);

}

void BackgroundWorker_RunWorkerCompleted(object sender,RunWorkerCompletedEventArgs e)
{
if(e.Cancelled)
lblStatus.Text =任务取消。
else if(e.Error!= null)
lblStatus.Text =Error - + e.Error.Message;
else
lblStatus.Text =任务完成...;

btnStartAsyncOperation.Enabled = true;
btnCancel.Enabled = false;
}

void BackgroundWorker_ProgressChanged(object sender,ProgressChangedEventArgs e)
{
progressBar1.Value = e.ProgressPercentage;
lblStatus.Text =Processing ......+ progressBar1.Value.ToString()+%;
}

void BackgroundWorker_DoWork(object sender,DoWorkEventArgs e)
{
for(int i = 0; i< 100; i ++)
{
System.Threading.Thread.Sleep(100);
backgroundWorker.ReportProgress(i);
if(backgroundWorker.CancellationPending)
{
e.Cancel = true;
backgroundWorker.ReportProgress(0);
return;
}
}
backgroundWorker.ReportProgress(100);
}

private void btnStartAsyncOperation_Click(object sender,EventArgs e)
{
btnStartAsyncOperation.Enabled = false;
btnCancel.Enabled = true;
backgroundWorker.RunWorkerAsync();
}

private void btnCancel_Click(object sender,EventArgs e)
{
if(backgroundWorker.IsBusy)
{
backgroundWorker.CancelAsync ();
}
}
}

按照在OP中的评论



在DoWork事件中,按如下方式设置字符串

  e.Result =你的字符串; 

和RunWorkerCompleted事件

  string muResult = Convert.ToString(e.Result); 

注意: e.Result是一个对象,所以你甚至可以设置具有多个属性的自定义类。


the Form does not displayed as Dialog inside backgroundworker?

Here is my code:

//From Form1
        private void backgroundWorkerResult_DoWork(object sender, DoWorkEventArgs e)
    {
        //do something here

        backgroundWorkerResult.ReportProgress(100);

        frmMessageBox frmMsgBox = new frmMessageBox();
        frmMsgBox.ShowDialog();
    }

Even though i already showed the frmMsgBox as a dialog i Can still click the Form1 that supposed to be not? how can fix this?

解决方案

I created a simple code sample that you can use to understand how the background worker works. Copy the code to a test form and add the following controls

  1. Label Control - Name it 'lblStatus'
  2. Progressbar Control - It should be named progressBar1.
  3. Add 2 buttons named 'btnStartAsyncOperation' and 'btnCancel' and link their click events.

Basically you should display the results in the RunWorkerCompleted event

    public partial class Form1 : Form
    {
        BackgroundWorker backgroundWorker;

        public Form1()
        {
            InitializeComponent();
            backgroundWorker = new BackgroundWorker {WorkerReportsProgress = true, WorkerSupportsCancellation = true};

            backgroundWorker.DoWork += new DoWorkEventHandler(BackgroundWorker_DoWork);
            backgroundWorker.ProgressChanged += new ProgressChangedEventHandler(BackgroundWorker_ProgressChanged);
            backgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(BackgroundWorker_RunWorkerCompleted);

        }

        void BackgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (e.Cancelled)
                lblStatus.Text = "Task Cancelled.";
            else if (e.Error != null)
                lblStatus.Text = "Error - " + e.Error.Message;
            else
                lblStatus.Text = "Task Completed...";

            btnStartAsyncOperation.Enabled = true;
            btnCancel.Enabled = false;
        }

        void BackgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            progressBar1.Value = e.ProgressPercentage;
            lblStatus.Text = "Processing......" + progressBar1.Value.ToString() + "%";
        }

        void BackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            for (int i = 0; i < 100; i++)
            {
                System.Threading.Thread.Sleep(100);
                backgroundWorker.ReportProgress(i);
                if (backgroundWorker.CancellationPending)
                {
                    e.Cancel = true;
                    backgroundWorker.ReportProgress(0);
                    return;
                }
            }
            backgroundWorker.ReportProgress(100);
        }

        private void btnStartAsyncOperation_Click(object sender, EventArgs e)
        {
            btnStartAsyncOperation.Enabled = false;
            btnCancel.Enabled = true;
            backgroundWorker.RunWorkerAsync();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            if (backgroundWorker.IsBusy)
            {
                backgroundWorker.CancelAsync();
            }
        }
    }

Edited as per comment by the OP

In the DoWork event, set your string as follows

 e.Result = "Your String";

and in the RunWorkerCompleted event

string muResult = Convert.ToString(e.Result);

NOTE: e.Result is an object so you can even set your custom classes with multiple properties in it.

这篇关于表单在后台工作程序中不显示为Dialog?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆