S
Stu
In this form I subscribe to an event which sends data from a device
about every 10 milliseconds which the form then graphs in its event
handler. In my form closing event I unsubscribe my listener, but I'm
still receiving some messages after this point and it's causing a
crash due to my graph object already being disposed. What is the best
way to prevent this from happening?
1) One thing I tried was keeping a lock which sets a disposing flag
in my FormClosing event, then in my MessageReceived event I lock and
check the disposing flag before continuing. However this is causing a
deadlock when I try to close my form because within the lock in my
MessageReceived event I am invoking stuff on the UI thread, which is
sitting in its FormClosing event waiting for the lock to be released.
2) I've also tried just wrapping the meat of the MessageReceived
event in a try/catch, which "works" but the error that's happening in
my graph is for some reason causing major funkiness in other graphs on
other forms after I close this one. I'm using
System.Windows.Forms.DataVisualization.Charting.Chart for my graphing,
and after catching this error any time I try to use another Chart
instance on *any* form I get "A first chance exception of type
'System.NullReferenceException' occurred in
System.Windows.Forms.DataVisualization.dll" which doesn't crash the
system, but also isn't catchable (it just prints that out to the debug
window) and it slows the program waaaaayyyyy down. Closing the
program and reopening fixes this.
3) Should I maybe just try a wait of some kind to give the messages a
second to finish sending after I unsubscribe the event? I don't
suppose there's any way to know that there are messages pending?
I have googled the crap out of this, it's one of those problems that
everyone has and no one seems to have an answer to. Thanks in advance
for your help.
about every 10 milliseconds which the form then graphs in its event
handler. In my form closing event I unsubscribe my listener, but I'm
still receiving some messages after this point and it's causing a
crash due to my graph object already being disposed. What is the best
way to prevent this from happening?
1) One thing I tried was keeping a lock which sets a disposing flag
in my FormClosing event, then in my MessageReceived event I lock and
check the disposing flag before continuing. However this is causing a
deadlock when I try to close my form because within the lock in my
MessageReceived event I am invoking stuff on the UI thread, which is
sitting in its FormClosing event waiting for the lock to be released.
2) I've also tried just wrapping the meat of the MessageReceived
event in a try/catch, which "works" but the error that's happening in
my graph is for some reason causing major funkiness in other graphs on
other forms after I close this one. I'm using
System.Windows.Forms.DataVisualization.Charting.Chart for my graphing,
and after catching this error any time I try to use another Chart
instance on *any* form I get "A first chance exception of type
'System.NullReferenceException' occurred in
System.Windows.Forms.DataVisualization.dll" which doesn't crash the
system, but also isn't catchable (it just prints that out to the debug
window) and it slows the program waaaaayyyyy down. Closing the
program and reopening fixes this.
3) Should I maybe just try a wait of some kind to give the messages a
second to finish sending after I unsubscribe the event? I don't
suppose there's any way to know that there are messages pending?
I have googled the crap out of this, it's one of those problems that
everyone has and no one seems to have an answer to. Thanks in advance
for your help.