|
-
July 16th, 2012, 04:53 PM
#1
[RESOLVED] C# .net BackgroundWorker.CancellationPending and while loop
Is the below bad practice?
Code:
while (!_backgroundWorker.CancellationPending)
{
// do a bunch of stuff
// done doing a bunch of stuff
break;
}
I can understand if the while loop said
Code:
while (false variable)
{
// do a bunch of stuff
// done doing a bunch of stuff
falseVariable = true;
}
If the background worker is not cancelled, you can't set _backgroundWorker.CancellationPending in order to get out of the while loop. So you have to break the while in order to get out. I could call _backgroundWorker.CancelAsync() but I don't want to cancel the background worker, I just want it to do all the stuff in this method unless the user hits the Cancel button which calls _backgroundWorker.CancelAsync().
I honestly don't see much of a difference. It just got under my skin when this new employee, whom is not a developer, tells me that it's bad practice to use the break statement, especially when she has never even seen the code i'm using.
-
July 18th, 2012, 02:28 PM
#2
Re: C# .net BackgroundWorker.CancellationPending and while loop
Why are you using a while loop in the first place? If you're just going to break out from the first iteration of the loop you might as well just use an if {}. Are you using a 'continue' somewhere above the break?
if (false variable) {
// Stuff
}
-Max
-
July 18th, 2012, 04:17 PM
#3
Re: C# .net BackgroundWorker.CancellationPending and while loop
It was a habit i picked up when learning the background worker a while back.
Since it's a asynchronous operation, I wanted it to stop wherever it was at when the background worker thread was cancelled.
In an if statement, I would have to constantly check the false variable to see if it changed to true before continuing.
Here's the the block I decided to go with. I could probably remove the cancellationPending portion but I'd rather just keep it there since everything is working fine now. By the way, this is in my DoWork method for the bgw:
Code:
var finished = false;
while ((!finished) && (!_uploadCancelled) && (!_fileBackgroundWorker.CancellationPending) && (!_isErrored))
{
if (dirs.Count() > 1)
{
try
{
var dirForm = new SelectCamdexDirectory(dirs);
dirForm.Show();
_sourceDirectory = dirForm.SourceDirectory;
ProcessUploadType(_sourceDirectory, dcImagesPath, imageCapturePath, uploadType, e);
}
catch (Exception ex)
{
OnError(ex, "Error in _fileBackgroundWorker_DoWork()");
DC.CleanupTables(_barcode);
throw;
}
}
else if (dirs.Count() == 1)
{
try
{
_sourceDirectory = new DirectoryInfo(dirs[0]);
ProcessUploadType(_sourceDirectory, dcImagesPath, imageCapturePath, uploadType, e);
}
catch (IOException ioex)
{
DC.CleanupTables(_barcode);
_isErrored = true;
}
catch (Exception ex)
{
OnError(ex, "Error in _fileBackgroundWorker_DoWork()");
DC.CleanupTables(_barcode);
_isErrored = true;
}
}
finished = true;// break the while loop if we get this far
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|