C# GUIDsometime just went bust.

  • Thread starter Thread starter Muscha
  • Start date Start date
M

Muscha

Hello,

I'm working on a GUI C# projects with a lot of threads doing many thing.
Basically the GUI is just a monitor of all the threads.

Often my application suddenly shut down without any warning. Noramlly if you
run from VS you get the Unhandled Exception dialog box, which does happen,
but occasionally it just stopped and seems like a normal termination (not).

Anyone has any suggestion about this issue?

thanks,

/m
 
Are you using Control.Invoke for thread to GUI communication?

When I'm building a class and I don't know if it'll be on the same thread or
not, I use

public void CallingMethod()
{
if( control1.InvokeRequired )
{
control1.Invoke( MyMethodHandler,...)
}
else
{
control1.MyMethod();
}
}

where MyMethodHandler is a delegate that holds MyMethod();
if this class is on a different thread than the GUI, it'll call the Invoke,
if it's place on the GUI thread, it'll just call MyMethod directly.
 
Hi Muscha,

As Chris suggested, if you are updating your controls from
other threads (other than the GUI thread that created your control),
you will need to need to use Control.Invoke() to marshal the call
correctly to the GUI thread.

One other thing you must probably try to do is handle
exceptions in the code that raises events.

foreach(YourDelegate del in YourEvent.GetInvocationList())
{
try
{
del(some params);
}
catch(Exception ex)
{
// Maybe the event handler threw an exception.
}
}

That way, you can trap exceptions even if your event handlers such as your
GUI app
terminates unexpectedly.

Regards,
Aravind C
 
Back
Top