Threading exception when closing form

  • Thread starter Thread starter Earl
  • Start date Start date
E

Earl

..Net 2.0 app. This is not behavior I saw in 1.0 or 1.1, so I'm a bit
puzzled...

Let's say I have form1 and form2. When I start the app, form1 shows. At some
point, form1 calls form2.show. If I now try to close or hide form1, I get a
threading exception. Anyone know how to avoid that without just leaving the
form in memory till the app closes?
 
Not sure I understand. You have this:

static void Main()
{
Applicatin.Run(new Form1());
}

And somewhere in Form1 you show an instance of Form2. You then Close Form1?
If that's the case the app should exit whether you're in CF 1.0 or 2.0
(there is no CF 1.1).

Post a simple example that reproduces the error. And what is the exact
exception? It might be that CF 2.0 throws when a worker thread tried to
affect UI, but agai I'm totally guessing since you only gave partial info.

-Chris
 
Chris,

That was exactly the case, I forgot that the app runs on that thread, what
threw me was that I saw the threading exception in the output before the app
exited (which is of course exactly what is happening). So secondary
question: Is there any startup scenario where I could close form1?
 
Yes. There is one and only one way.

static void Main()
{
Applicatin.Run(new Form1());
Applicatin.Run(new Form2());
}

Close Form1 and Form2 will appear as it gets its own message pump.

-Chris
 
Ahhhhhhh ... excellent! Given that my startup is a splash/login, this is a
perfect way to go. Thanks Chris.
 
I experimented with that (VB.Net) and had some interesting results. If I try
to show form2 from form1, then close form1, the app still quits. Also, if I
do not close form1, and if I then call application.exit from form2, only at
that point does form1 close and does the application run form2. I'm
apparently not calling these in the proper order?
 
Back
Top