Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > .NET General

Vista - "InvalidOperationException" when closing a form (using VS2008)

Reply
 
Old 02-05-2009   #1 (permalink)
Jonathan Jones


 
 

"InvalidOperationException" when closing a form (using VS2008)

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

My System SpecsSystem Spec
Old 02-05-2009   #2 (permalink)
Josh Einstein


 
 

Re: "InvalidOperationException" when closing a form (using VS2008)

Try this:

BeginInvoke( new Action(Close) )

Josh

"Jonathan Jones" <JonathanJones@xxxxxx> wrote in message
news3A127AB-B74B-48C5-8BD6-316FA8F5C6FC@xxxxxx
Quote:

> 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
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Most games closing, the message says it has "stopped working" Gaming
"...must be placed inside a form tag with runat=server" error .NET General
(Vista 64-bit) Can one somehow "dump" a program's memory (e.g., firefox or chrome) to a file and load after closing a program? Vista file management
Not responding when closing "Local Area Connection Properties" box Vista networking & sharing
"Reclaim space on closing" Error Live Mail


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46