进度条对话框最小化主窗体

时间:2013-08-01 11:28:44

标签: c# backgroundworker

我是基于.NET桌面开发的新手,所以请原谅我,如果我错过了一些小事。

我有一个Main表单,当用户单击一个按钮时,会打开一个对话框表单,用户可以选择一些文件,在选择文件后,用户单击此表单上的Process按钮开始一个漫长的过程。 所以我所做的就是单击按钮,我隐藏这个表单并启动一个后台工作人员,并开始显示另一个对话框来显示一个动画进度条图像。 我面临的问题是当用户单击“处理”按钮时,表单被隐藏,并且显示了进度条对话框,但主窗体被最小化。任何身体都可以帮助我吗?我不希望主窗体最小化。

我选择文件的对话框的代码如下:

frmProgressbar progressDialog = null;

private void btnProcess_Click(object sender, EventArgs e)
{
    this.Hide();
    progressDialog = new frmProgressbar();
    worker = new BackgroundWorker();
    worker.WorkerReportsProgress = true;
    worker.WorkerSupportsCancellation = true;
    worker.DoWork += new System.ComponentModel.DoWorkEventHandler(worker_DoWork);
    worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(
            worker_RunWorkerCompleted);
    worker.RunWorkerAsync();
    progressDialog.ShowDialog();
}
void worker_DoWork(object sender, DoWorkEventArgs e)
{
    // Do the lengthy work
}
void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    progressDialog.Close();
}

2 个答案:

答案 0 :(得分:0)

尝试将父表单提供给ShowDialog方法

progressDialog.ShowDialog(this);

答案 1 :(得分:0)

     private Thread progressThread;

 private void Btn_Upload_Click(object sender, EventArgs e)
    {

        Thread.CurrentThread.Priority = ThreadPriority.Highest;
        progressThread = new Thread(new ThreadStart(ShowProgressWindow));
        progressThread.Priority = ThreadPriority.Lowest;
        progressThread.Start(); 

     // do your operation here...
        Class_Upload_SSRD.Search_All_Ptags(Cbox_Ptag_names, label4);
  ///close porcess once ur work is done...
        progressThread.Abort();
        Thread.CurrentThread.Priority = ThreadPriority.Normal;
        this.Show();
    }

//进度条功能

   public void ShowProgressWindow()
    {
        Progress_bar pb = new Progress_bar();
        pb.ShowDialog();
    }

创建进度窗口窗体,当操作被执行时,它将如图所示可见 enter image description here