Need help ... how to find if form is closed?

M

Michael Gorbach

Iv got a little bit of a threading problem.
Iv got a form which handles events fired by an object running on
another thread (the event handlers are executed on a thread differnet
from the form's gui thread). Im using BeginInvoke to call methods on
the form that manipulate its GUI elements, as is reuqired. The problem
is this: When that particular form is closed and the object is still
running, the events still fire and get caught by the event handlers.
The BeginInvoke call fails with an InvalidOperationException (something
about the window handle not being created). Although i can catch this
excpetion and ignore it, doing this would slow down my prgoram alot and
would break the guideline of not having exceptions thrown during normal
program execution (it is normal for this form to be closed, as it is a
helper form). How do i check if the form is open and has a window
associated with it?
 
B

Bernhard Huemer

Hello,

I would recommend you to handle the Form's Closed event. Your handler
simply detaches every registered event handler which gets executed on your
form.

greets
 
M

Michael Gorbach

ah ... great idea. I dont know why i didnt think of that.
For some reason the first thing comming into my head was to check in
the method that calls the begininvokes.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top