I have a project in C#/Windows Forms, where I have a simple form with a PictureBox that i draw images to from a background thread. If I close the form while drawing is active the background thread crashes when trying to perform invoke to update the bitmap, which is quite logical. I tried putting an AutoResetEvent that the destructor would wait on, so that destructor would not return until all drawing is done and the background thread knows it is supposed to exit. But I concluded that the crash happens before destructor is executed so I started to wonder, at what point are the components of the form destroyed? I assume that before the destructor? So what is a safe place to wait for the drawing to finish? Override Dispose() and do it there?
I have a project in C#/Windows Forms, where I have a simple form with
Share
That is what I would think, there is already a
Dispose(bool disposing)method in the Designer file e.g.MainForm.Designer.cs.I would put some code to close/finish the background thread in the beginning of that method. And a
AutoResetEventor aManualResetEventwould be a good way to do it.