NDraskovic
NDraskovic

Reputation: 706

Closing the form when backgroundWorker stops working

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?

Upvotes: 4

Views: 2343

Answers (5)

Ahmed KRAIEM
Ahmed KRAIEM

Reputation: 10427

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

Upvotes: 1

user1552814
user1552814

Reputation: 31

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

Upvotes: 1

yonan2236
yonan2236

Reputation: 13659

Add RunWorkerCompleted event from your backgroundWorker:

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

Upvotes: 1

John Willemse
John Willemse

Reputation: 6698

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.

Upvotes: 4

Matthew Watson
Matthew Watson

Reputation: 109862

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.

Upvotes: 3

Related Questions