views:

36

answers:

2

Hi, I believe I have a potential threading issue. I have a user control that contains the following code:

    private void btnVerify_Click(object sender, EventArgs e)
    {
        if (!backgroundWorkerVerify.IsBusy)
        {
            backgroundWorkerVerify.RunWorkerAsync();
        }
    }

  private void backgroundWorkerVerify_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
    {
        VerifyAppointments();
    }

  private void backgroundWorkerVerify_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
    {

        MessageBox.Show("Information was Verified.", "Verify",
            MessageBoxButtons.OK, MessageBoxIcon.Information);
        CloseEvent();
    }

vanilla code. but the issue I have is that when the application is running and the users tabs to another application when they return to mine the application is hung, they get a blank screen and they have to kill it. This started when I put the threading code. Could I have some rogue threads out there? what is the best way to zero in a threading problem? The issue can't be recreated on my machine...I know I must be missing something on how to dispose of a backgroundworker properly. Any thoughts are appreciated, Thanks,

Jose

A: 

Are you accessing the GUI from the VerifyAppointments() method? You should utilize the DoWorkEventArgs in order to pass in the arguments you are verifying and you should not access the GUI from the BackgroundWorker directly.

You can safely access the GUI only in the RunWorkerCompleted or the ProgressChanged events.

Lirik
Lirik, I had that issue in the beginning and I got the nasty message about not being able to access the elements on this thread, so I covered that base. Thanks, though.
joseluisrod
A: 

Your code snippet doesn't explain it, but deadlocking the UI thread is never that difficult when you use BGW and are interested in its IsBusy property. A deadlock like this is usually easy to diagnose, use Debug + Break All. Then Debug + Windows + Threads and double-click the Main Thread. Then Debug + Windows + Call Stack to see what the UI thread is doing.

The common scenario is that the UI thread is looping on the IsBusy property. The BGW can't complete because its RunWorkerCompleted event can't run until the UI thread goes idle.

Hans Passant
Yep. I will investigate. I found some attempts to start the threads twice. all kinds of weirdness. I took the BGW out for the users' sake, I will reintroduce them w/care. Thanks for the pointers.
joseluisrod