Groups | Blog | Home
all groups > dotnet windows forms > april 2007 >

dotnet windows forms : Problems with BackGroundWorker


CH
4/13/2007 1:53:42 AM
Hy,
I have a backgroundworker doing some work when the user presses a
button, the problem is when I quickly start and stop the
backgroundworker, after 3-4 starts the DoWork event doesn't fire
anymore.
What usualy happends is this:
1. start the worker
2. immediately stop the worker (everything is OK)

3. immediately start the worker
4. immediately stop the worker (everything is OK)

5 immediately start the worker (the event handler for DoWork is not
called)

Anyone got any ideas on what happens?

Thanks
CH
4/13/2007 3:49:44 AM
I think the threads blocks somewhere after
workerthread.RunWorkerAsync() and before the DoWork event handler.
Is this possible
Andy
4/13/2007 1:41:41 PM
[quoted text, click to view]

How are you canceling? Without any of your code, it will be hard to
tell..
CH
4/14/2007 3:02:16 AM
Thank you for the reply here are pieces of my code (he relevant ones:)

//
//runGraphs
//
this.mrunGraphs = new BackgroundWorker();
this.mrunGraphs.WorkerReportsProgress = true;
this.mrunGraphs.WorkerSupportsCancellation = true;
this.mrunGraphs.DoWork += new
DoWorkEventHandler(runGraphs_DoWork);
this.mrunGraphs.ProgressChanged += new
ProgressChangedEventHandler(runGraphs_ProgressChanged);
this.mrunGraphs.RunWorkerCompleted += new
RunWorkerCompletedEventHandler(runGraphs_RunWorkerCompleted);


void buttonAnalyze_Click(object sender, EventArgs e)
{
this.theLog.WriteMessage("Analisys started");

this.DisableMainPanel();

if (mrunGraphs.IsBusy) theLog.WriteError("We are busy");

this.mrunGraphs.RunWorkerAsync();//send DoWork event //
LINE A

}

void runGraphs_DoWork(object sender, DoWorkEventArgs e)
{
e.Result = new ArrayList(); //LINE B
this.RunShit((ArrayList)e.Result,e);
}


void runGraphs_RunWorkerCompleted(object sender,
RunWorkerCompletedEventArgs e)
{
this.EnableMainPanel();

if (e.Error != null)
{
LogWriter.WriteError(e.Error.Message);
return;
}
}

void runGraphs_ProgressChanged(object sender, ProgressChangedEventArgs
e)
{
AdvancedStatus CurrentStatus =
(AdvancedStatus)e.UserState;

this.manalysingForm.UpdateBar(CurrentStatus.Progress);

this.manalysingForm.UpdateBarOverall(CurrentStatus.OverallProgress);
}


//button1 is on the status reporting form mfather is the main form
private void button1_Click(object sender, EventArgs e)
{
this.mfather.StopWorkerThread();
this.Hide();
}

public void StopWorkerThread()
{
this.mrunGraphs.CancelAsync();
}

Somewhere between LINE A and LINE B the thread blocks

Hope you can find the problems,
Thanks,
Dan Ionut Fechete
AddThis Social Bookmark Button