"InvalidOperationException" when closing a form (using VS2008)

J

Jonathan Jones

Good Afternoon,

This problem has been plaguing me most of the afternoon. I have an
application that interfaces with an external device and reports status.
There is a requirement that the application be able to save outgoing messages
with unique names so when the TCP client sends a message, I created an event
that the main application responds to. In the body of that event, I create
an instance of my formand display it for the user. The user can choose to
ignore this particular message by pressing the "Discard" button or keep it by
pressing the "Keep" button.

If the user presses the "Discard" button, I just want to close the form.
When I call

this.Close()

I get an InvalidOperationException which I understand to mean that the form
is trying to close on a thread that did not create it. How do I get this to
work? I am familiar with accessing controls correctly using delegates but I
can't seem to get this to work properly.

Would I have to raise an event back to the main application so it can close
the form? If so, do I have to create the new form in it's own thread?

Thanks!

Jonathan
 

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