"InvalidOperationException" when closing a form (using VS2008)

  • Thread starter Thread starter Jonathan Jones
  • Start date Start date
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
 
Back
Top