When working with multithreading in VB Forms, I have a pretty consistent problem:
Dim MyThread As New System.Threading.Thread(AddressOf _SomeThreadSub)
MyThread.IsBackGround = True
According to the documentation, when I set IsBackground = True the Thread should die when the owning Object does (in this case it’s a System.Windows.Forms.Form, but it happens in ALL Classes). So when a Thread is encapsulated in a Form and I set it
IsBackground, I trust the documentation and expect the thread to die on shutdown. Not usually the case, though.
I mostly use Threads for big processes, like downloading a lot of data from a remote database and writing it to a local one. In these cases I need progress bars so the end user doesn’t have to sit around wondering what’s happening, so the Forms containing
the Threads have Delegates that update ProgressBars.
When closing the Form, Disposing the Object, etc there is almost always a System.Exception at runtime; when there is such an Exception it is always in a Delegate and it’s always trying to refer back to the Disposing/Disposed Object and telling me the reference
is either “not set to an instance” or else “cannot access a disposed object.”
I’ve built some pretty stupidly elaborate constructs to get around this, the easiest of which is to create a Class Scoped boolean switch indicating whether the thread should abort. The problem is that this simplest method requires cutting/pasting a
pantload of If-Then-Else checks into my multithreaded subs and functions and even delegates so that right before anything is done there’s a check to see if the thread should be aborting so the rest can be ignored; this still doesn’t always work because sometimes
the check can pass right before the object call but the object call itself happens right after disposal; this is also a performance problem (and frankly it’s stupid code) because I’m running the same If-Then-Else about every 3rd line in hundreds of lines of
So if IsBackground doesn’t work as advertised, how do I make sure any background threads properly die before the object owning/containing them is disposed or uninstantiated?
Hi Andrew,There’s no way that _I_ could ever show YOU something you didn’t know! But I will add this anyway:This isn’t dissimilar to the user shutting down a program when a background worker is still running and I’ve struggled with that several times.What I’ve come to do – and it seems to work (emphasis on
Is it an option to just kill the background process in the dispose/destructor of the form rather than relying on the thread to do it for you. (Regardless of whether or not it should, it doesn’t, so just kill it yourself…) You will need to have
Hi Andrew,There’s no way that _I_ could ever show YOU something you didn’t know! But I will add this anyway:Et tu, Frank L. Smith?I catch a lot of ____ on webforums everywhere because I’m blunt and I place a high value on accuracy, clarity, and relevance; anyone who spends the 5-10 minutes required to actually read a dozen posts of mine back will see that when I make a mistake (whether