Several forms each running on its own thread ?

  • Thread starter Thread starter Steve Terepin
  • Start date Start date
S

Steve Terepin

My application needs to have three Forms, each of which runs on its own thread ie with its own independent message loop etc. I've implemented this by creating three Thread instances, where the ThreadStart delegate points to a method that does something like this :

void MyThreadStart ( )
{
System.Windows.Forms.Application.Run(
new MyForm() // Different MyForm class for each thread
) ;
}

This works fine most of the time, but just occasionally (perhaps one time in ten) an exception gets thrown in the InitializeComponent method called by my Form's constructor. The error message tells me that a ThreadStateException has occurred. So maybe 'Application.Run' isn't the right way to do this. Any ideas, anyone ?

Thanks,

Steve.
 
Steve Terepin said:
My application needs to have three Forms, each of which runs
on its own thread ie with its own independent message loop etc.

I am just curious why you need that. Maybe there is a better solution...
 
Good question ! I'm developing a UI that will be packaged as a COM
component that will be used from a native C++ Win32 app. The UI needs to
have its own thread, so as not to hold up progress of the 'main' app which
is capturing real time data. And in fact there are three different kinds of
UI display, activated at different times. For debugging and testing, I have
a .Net console application that creates instances of all three UI forms and
sends them the same string-oriented messages that will eventually be coming
in from the native C++ app.

Yes, it is a bit convoluted ! But is there a good reason for *not* having
more than one thread servicing a UI ?

S.
 
Steve,

I'm also facing this same problem. I want three different displays (maybe
more down the road) and the data I'm receiving is from a real time source. I
read it into a Main form but then want to display various waterfall displays
on various forms. Is the best option making various threads for each form?
I'm also thinking of using Managed DirectX for a few of the forms, and the
seperate thread would be nice to create a render loop. I want the user to be
able to turn dispalys on and off as each of the displays update. I apprecaite
any help on how to set up my project's structure.

Nicholas Nezis
 
Back
Top