I’m trying to find a really strange bug in my application and while looking for it I’ve come across a doubt about how BackgroundWorker works.
If I have a code that looks like this:
var bg = new BackgroundWorker();
bg.DoWork += MyHandler;
bg.RunWorkerAsync
Is there anyway in which that would never run? What I have in mind is the bg getting out of scope and being claimed by the BG before it can actually get a chance to run.
Could that happen?
If in the next line an exception will happen, probably the DoWork will not be called. But I don’t assume this is your problem. Otherwise no, RunWorkerAsnyc will trigger the execution in any way, GC will not garbage the instance also if you don’t have no more reference to your BackgroundWorker.
Check the AsyncCompletedEventArgs.Error property, to see if an exception has happened in the DoWork-eventhandler.