Skip to content

BackgroundWorker.isRunning

Sat Feb 11 2006

This is a private boolean member of the new BackgroundWorker class in .NET 2.0. If it were public, perhaps there would be less questions like this one on the gotdotnet messageboards. I ran into a similar problem recently and decided that instead of catching an exception when the BackgroundWorker is running, I would emulate the isRunning member myself. Since this is multithreaded by its nature, I decided to use a lock object to control access to a static boolean member in the class that uses the BackgroundWorker component.

csharp
static bool isRunningBgWorker = false;
static object lockObj = new object();

...

if (!isRunningBgWorker) {
	this.backgroundWorker1.RunWorkerAsync();
}

I check to see if it's running before even setting it off to do the work.

csharp
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) {
	lock (lockObj) {
		isRunningBgWorker = true;
	}

The first thing that happens when the DoWork eventhandler is called and

csharp
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) {
...

	lock (lockObj) {
		isRunningBgWorker = false;
	}
}

the last thing when the work is completed: controlled access to the isRunningBgWorker member. Hope this helps somebody else out there who is having trouble with the BackgroundWorker component.

💾 May the source be with you. v3.2.419