Ok, here's the overview. I'm writing an application which (among other things) can do repetitive writes to the serial port via a timer (the timer runs on a 20 ms period or so, and a write occurs when the timer runs out (over and over). Everthing works fine, except if you try to terminate the application (via the little x) while the continous write is going on. If you do so, you get this error:
Now, as best as my VB.net-newbie mind can comprehend, this is happening because the thread attacking the serial port is connected to the main frame, and when the frame dissappears, the serial port thread attempts to reference it before .Net can kill it, thus throwing the exception.
Towards this end, I've hooked the "close" signal of the main frame, and done everything possible to stop the serial port... including closing the port, stopping the autowrite timer, purging the in/out buffers, disabling the port's events, setting the serial port object equal to "Nothing", and waiting a long time... but it always gives me the exception, except for one case.
If I hook the "close" signal of the main frame and run a MsgBox... no closing of the serial port, no disabling of the port's events, no nothing, just a msgbox before the window closes (you can even see the main frame updating frantically as the serial port does its thing behind the messagebox)... it works fine - no exceptions.
So that's where I am. Any thoughts/ideas/wild conjectures on how I can avoid the exception without the strange MsgBox workaround?
-Nate
VB.NET:
An unhandled exception of type 'System.ObjectDisposedException' occurred in system.windows.forms.dll
Additional information: Cannot access a disposed object named "Form1".
Now, as best as my VB.net-newbie mind can comprehend, this is happening because the thread attacking the serial port is connected to the main frame, and when the frame dissappears, the serial port thread attempts to reference it before .Net can kill it, thus throwing the exception.
Towards this end, I've hooked the "close" signal of the main frame, and done everything possible to stop the serial port... including closing the port, stopping the autowrite timer, purging the in/out buffers, disabling the port's events, setting the serial port object equal to "Nothing", and waiting a long time... but it always gives me the exception, except for one case.
If I hook the "close" signal of the main frame and run a MsgBox... no closing of the serial port, no disabling of the port's events, no nothing, just a msgbox before the window closes (you can even see the main frame updating frantically as the serial port does its thing behind the messagebox)... it works fine - no exceptions.
So that's where I am. Any thoughts/ideas/wild conjectures on how I can avoid the exception without the strange MsgBox workaround?
-Nate