473,322 Members | 1,409 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

C# Forms App threading and events

2
I am new to writing multi-threaded code and am working on a Windows forms MDI application. There is an MDI Form that can have 0 to many MDI Children. The MDI form starts a thread that is used to poll a device via a USB port. When new data is available from the device, the thread raises an event that the child forms can subscribe to.

The event handlers in the child forms update themselves based on the new data that has arrived from the device. Since the event handler is called from the communication thread and not the UI thread, the code that updates the form must be "Invoked".

In the FormClosing event of the child form, I unsubscribe to the event before the form closes. But I often get an error because the communication event handler in the child form is getting called after the form has closed and been disposed.

I put a bunch of trace statements in to figure out what was going on and found that with several forms subscribed to the the event, the events could get queued up but I am able to close one of the forms before its event handler had been called. The form closing event occurs, the event handler is unsubscribed and the form is closed. However since the event handler has already been queued, the CLR (?) attempts to call the event handler on the disposed form.

I'm sure this is a common issue but I haven't been able to find an answer so far.
Sep 27 '07 #1
0 1038

Sign in to post your reply or Sign up for a free account.

Similar topics

77
by: Jon Skeet [C# MVP] | last post by:
Please excuse the cross-post - I'm pretty sure I've had interest in the article on all the groups this is posted to. I've finally managed to finish my article on multi-threading - at least for...
4
by: Bardo | last post by:
Hi, I have a situation where I am capturing both a WMI event utilising the "ManagementEventWatcher" in the "System.Management" namespace, and a corresponding event ("EntryWritten") raised from...
8
by: Scott | last post by:
I have written windows applications using MFC for several years and have frequently used MFC techniques. Now I'm moving to C# .NET WinForm. Mostly C# books describes C# language (sometimes...
6
by: Dan | last post by:
I've created a pocketpc app which has a startup form containing a listview. The form creates an object which in turn creates a System.Threading.Timer. It keeps track of the Timer state using a...
5
by: Andrew Lippitt | last post by:
What gaurantees are there in the way of which thread the events are called from. I've seen: Thread A Begin Thread B Begin Thread A End Thread A End That seems to indicate that Begin and End...
0
by: BenLeino | last post by:
Hi out there, I have a little problem with threading an event receiving. I have a custom Class (DLL) that raises Events. When I run the Instance without threading it works fine. When I do...
2
by: Richard Bell | last post by:
I'm working on a VB.net app that uses IE, some forms, and multiple execution threads. I'm unclear on a couple of threading related matters and hoped that someone could provide some insight. ...
13
by: Bob | last post by:
My WinForms app runs on the single default thread, and uses a single SqlConnection object for all queries. I need to use one or more timers to periodically execute some of them. My own testing...
2
by: Anatoli | last post by:
I've got an incrementTimer()-Function, which is called on mous click. It increments Windows.Forms.Timer interval and starts the timer... No problem so far. Now I want to do the same when...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.