Closing the form when backgroundWorker stops working

孤街醉人 提交于 2019-12-10 15:35:20

问题


I'm working on an application that should read the data from Mifare smart card. I have to create a form will check the Mifare reader periodically and when the card is in range, read its serial number and send it to the parent form. I managed to get the background worker to read the serial number, but I can't close the form from it due to cross thread calling error it would cause. Is there a way to monitor the work that backGroundWorker does, and when it successfully reads the card ID, to stop it and close the child form? This is the code I'm using in the DoWork method:

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();
    }
}

This code does it's job, but I have to manually close the form. Is there a way to check if the CardID variable changes it's value in the main thread and if it does, close the form. I tried to solve this problem by using a timer, but when I do that, the timer blocks the main form thread, and I can't close it manually (which of course I have to be able). Can you please suggest a way to solve this problem?


回答1:


You can use the BackgroundWorker.RunWorkerCompleted event to monitor when the BackgroundWorker is done.

Occurs when the background operation has completed, has been canceled, or has raised an exception.

From there, you could close the form programmatically.




回答2:


You can write your BackgroundWorker so that it returns when it has finished its work.

Then in your main form, subscribe to the BackgroundWorker.RunWorkerCompleted and respond appropriately, presumably by just closing the form.




回答3:


Add RunWorkerCompleted event from your backgroundWorker:

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



回答4:


You can avoid cross-thread calling error by checking InvokedRequired and try to BeginInvoke using delegates.




回答5:


As others mentioned, here is how to implement the BackgroundWorker.RunWorkerCompleted event:

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.
    }


来源:https://stackoverflow.com/questions/16770337/closing-the-form-when-backgroundworker-stops-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!