当backgroundWorker停止工作时关闭表单

时间:2013-05-27 09:38:50

标签: c# multithreading backgroundworker

我正在开发一个应该从Mifare智能卡读取数据的应用程序。我必须创建一个表单,定期检查Mifare读卡器,当卡在范围内时,读取其序列号并将其发送到父表单。我设法让后台工作者读取序列号,但由于它会导致的跨线程调用错误,我无法从中关闭表单。有没有办法监控backGroundWorker的工作,以及何时成功读取卡ID,停止并关闭子表单? 这是我在DoWork方法中使用的代码:

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
    BackgroundWorker worker = sender as BackgroundWorker;
    while (!worker.CancellationPending)
    {
        MifareReader.CommPort = 4;
        MifareReader.PortOpen = true;
        MifareReader.mfAutoMode(true);               
        MifareReader.mfRequest();                
        if (CardID == "0" || CardID == string.Empty)
        {
            MifareReader.mfRequest();
            CardID = MifareReader.mfAnticollision().ToString();
            MifareReader.mfHalt();
        }
        else if (CardID != "0" && CardID != string.Empty)
        {
            MessageBox.Show(ObrnutiID);
            worker.CancelAsync();                    
        }
        MifareCitac.mfHalt();
    }
}

此代码完成了它的工作,但我必须手动关闭表单。有没有办法检查CardID变量是否在主线程中更改了它的值,如果是,则关闭表单。 我试图通过使用计时器解决这个问题,但是当我这样做时,计时器阻止主表单线程,我不能手动关闭它(当然我必须能够)。你能建议一种解决这个问题的方法吗?

5 个答案:

答案 0 :(得分:4)

您可以使用BackgroundWorker.RunWorkerCompleted事件来监控BackgroundWorker何时完成。

  

在后台操作完成,已取消或引发异常时发生。

从那里,您可以以编程方式关闭表单。

答案 1 :(得分:3)

您可以编写BackgroundWorker,以便在完成工作后返回。

然后在您的主要表单中,订阅BackgroundWorker.RunWorkerCompleted并做出适当回应,大概只需关闭表单即可。

答案 2 :(得分:1)

从backgroundWorker添加RunWorkerCompleted个事件:

private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    this.Close() // Closes the form.
}

答案 3 :(得分:1)

您可以通过检查InvokedRequired并使用委托尝试使用BeginInvoke来避免跨线程调用错误。

答案 4 :(得分:1)

正如其他人所提到的,以下是如何实施BackgroundWorker.RunWorkerCompleted事件:

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
    bool found = false;
            var worker = sender as BackgroundWorker;
    while (!worker.CancellationPending && !found)
    {
        MifareReader.CommPort = 4;
        MifareReader.PortOpen = true;
        MifareReader.mfAutoMode(true);               
        MifareReader.mfRequest();                
        if (CardID == "0" || CardID == string.Empty)
        {
            MifareReader.mfRequest();
            CardID = MifareReader.mfAnticollision().ToString();
            MifareReader.mfHalt();
        }
        else
        {
            e.Result = ObrnutiID;
            found = true;
            MifareCitac.mfHalt();
        }
    }
    if (worker.CancellationPending)
    {
        e.Cancel = true;
    }
}

    private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        if (e.Cancelled)
        {
            // The user canceled the operation.
            MessageBox.Show("Operation was canceled");
        }
        else if (e.Error != null)
        {
            // There was an error during the operation. 
            string msg = String.Format("An error occurred: {0}", e.Error.Message);
            MessageBox.Show(msg);
        }
        else
        {
            // The operation completed normally. 
            string msg = String.Format("Result = {0}", e.Result);
            MessageBox.Show(msg);
        }
        this.Close() // Closes the form.
    }