检查,看看是否在另一种形式中一个线程仍在运行 [英] Check to see if a thread in another form is still running

查看:203
本文介绍了检查,看看是否在另一种形式中一个线程仍在运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有涉及两种形式的Windows窗体应用程序。子窗体被用于将数据导出到CSV文件,并使用一个后台工作写入文件。虽然这是发生我有如下形式隐藏。而背景工人正在运行,使用户可以退出应用程序,即使当背景工人写入文件
的母体形式仍然有效。在父窗体我已经加了的FormClosing事件处理程序提示用户,如果后台工作仍在运行。
我遇到正在访问的父窗体背景工人的问题。这里是我试过...

 私人无效MainForm_FormClosing(对象发件人,FormClosingEventArgs E)
{
ExportForm电子表格=新ExportForm(GridView控件中,TableName,GridProgressBar,ProgressLabel);

如果(eForm.PartWorker.IsBusy ==真)
MessageBox.Show(忙);
}



这个问题将是,它是创建子窗体的新实例,所以后台工作永远不会有真正的为它的IsBusy属性。我怎么能访问此背景工人在我父的形式,所以我可以检查,看看是否满足此条件。



下面是PartWorker BackgroundWorker的代码...

 #地区PartWorker活动

无效PartWorker_DoWork(对象发件人,DoWorkEventArgs E)
$ { b $ b GetSwitch();
INT batchNum = 0;
布尔做= FALSE;
ProgressLabel.Visible = TRUE;

,而(做!)
{
的for(int i = 1; I< = 100;我++)
{
Thread.sleep代码( 100);
PartWorker.ReportProgress(ⅰ);
}

进行= Export.ExportPartition(SAVEFILE,数据表,50000,batchNum ++);
}
}

无效PartWorker_ProgressChanged(对象发件人,ProgressChangedEventArgs E)
{
Progress.Style = ProgressBarStyle.Blocks;
Progress.Value = e.ProgressPercentage;
//可以把它放到了这里正在写入的文件名。
ProgressLabel.Text =写文件:+ e.ProgressPercentage.ToString()+%完成;
}

无效PartWorker_RunWorkerCompleted(对象发件人,RunWorkerCompletedEventArgs E)
{
Progress.Value = 100;
ProgressLabel.Visible = FALSE;
Progress.Visible = FALSE;
MessageBox.Show(文件成功创建!,保存的文件,MessageBoxButtons.OK,MessageBoxIcon.Information);
PartWorker.Dispose();
this.Close();
}
#endregion


解决方案

保持在主窗体的子窗体的引用:

 类的MainForm:表格{
私人exportForm exportForm;

//无论在创建子窗体分配exportForm
}

接下来,在你的 ExportForm ,创建表示形式还在忙着属性。结果
这比访问一个更好的办法了的BackgroundWorker (阅读:封装)。

 类ExportForm:表格{
酒店的公共布尔IsBusy {
{返回this.PartWorker.IsBusy; }
}
}



然后通过访问做的主要形式检查新创建的属性:

 无效MainForm_FormClosing(对象发件人,FormClosingEventArgs E)
{
如果(这.exportForm.IsBusy)
MessageBox.Show(忙);
}


I have a Windows Form application that involves two Forms. The child form is used to export data into CSV files, and uses a background worker to write the file. While this is occurring I have the form hidden. The parent form is still active while the background worker is running, so the user can exit the application, even when the background worker is writing files. On the parent form I have added a FormClosing event handler to prompt the user if a background worker is still running. The problem I am encountering is accessing the background worker in the parent form. Here is what I tried...

    private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
    {
        ExportForm eForm = new ExportForm(GridView, TableName, GridProgressBar, ProgressLabel);

        if (eForm.PartWorker.IsBusy == true)
            MessageBox.Show("Busy");
    }

The problem will this is that it is creating a new instance of the Child Form, so the background worker will never have true for it's IsBusy attribute. How could I access this background worker in my parent form so I can check to see if this condition is true.

Here is the code for the PartWorker BackgroundWorker...

    #region PartWorker Events

    void PartWorker_DoWork(object sender, DoWorkEventArgs e)
    {
        GetSwitch();
        int batchNum = 0;
        bool done = false;
        ProgressLabel.Visible = true;

        while (!done)
        {
            for (int i = 1; i <= 100; i++)
            {
                Thread.Sleep(100);
                PartWorker.ReportProgress(i);
            }

            done = Export.ExportPartition(SaveFile, DataTable, 50000, batchNum++);
        }
    }

    void PartWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        Progress.Style = ProgressBarStyle.Blocks;
        Progress.Value = e.ProgressPercentage;
        //May want to put the file name that is being written here.
        ProgressLabel.Text = "Writing File: " + e.ProgressPercentage.ToString()  +"% Complete";
    }

    void PartWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        Progress.Value = 100;
        ProgressLabel.Visible = false;
        Progress.Visible = false;
        MessageBox.Show("Files sucessfully created!", "Files Saved", MessageBoxButtons.OK, MessageBoxIcon.Information);
        PartWorker.Dispose();
        this.Close();
    }
    #endregion

解决方案

Hold a reference to the child form in the main form:

class MainForm : Form {
    private ExportForm exportForm;

    // assign exportForm wherever the child form is created
}

Next, in your ExportForm, create a property that indicates that the form is still busy.
This is a better approach than accessing its BackgroundWorker (read: encapsulation).

class ExportForm : Form {
    public bool IsBusy {
        get { return this.PartWorker.IsBusy; }
    }
}

Then do the check in the main form by accessing the newly created property:

void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
    if (this.exportForm.IsBusy)
        MessageBox.Show("Busy");
}

这篇关于检查,看看是否在另一种形式中一个线程仍在运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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