检查另一种形式的线程是否仍在运行

发布于 2024-11-28 14:39:39 字数 1918 浏览 2 评论 0原文

我有一个涉及两个窗体的 Windows 窗体应用程序。子表单用于将数据导出到 CSV 文件,并使用后台工作者写入文件。当这种情况发生时,我隐藏了表格。 当后台工作程序运行时,父窗体仍然处于活动状态,因此即使后台工作程序正在写入文件,用户也可以退出应用程序。在父表单上,我添加了一个 FormClosing 事件处理程序,以提示用户后台工作人员是否仍在运行。 我遇到的问题是访问父表单中的后台工作人员。这是我尝试过的...

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

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

问题是它正在创建子表单的新实例,因此后台工作人员永远不会拥有 true 的 IsBusy 属性。我怎样才能在我的父表单中访问这个后台工作人员,以便我可以检查这个条件是否成立。

这是 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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

孤独患者 2024-12-05 14:39:39

在主窗体中保留对子窗体的引用:

class MainForm : Form {
    private ExportForm exportForm;

    // assign exportForm wherever the child form is created
}

接下来,在 ExportForm 中,创建一个属性来指示该窗体仍处于忙碌状态。
这是比访问其 BackgroundWorker (阅读:封装)更好的方法。

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

然后通过访问新创建的属性在主窗体中进行检查:

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

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");
}
独闯女儿国 2024-12-05 14:39:39

创建一个包含后台工作人员或后台工作人员集合(如果您可能有多个)的单例业务对象。将您的后台工作人员从子表单创建到此单例中,应用程序域中的所有人都可以访问它。

Create a singleton business object which holds the background worker, or a collection of background workers (if you may have multiple). Create your background worker from the child form into this singleton and it will be accessible to all in the app domain.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文