Open winforms with Async threads  
Author Message
TMF





PostPosted: Windows Forms General, Open winforms with Async threads Top

Hi,

On my application 13 forms, that do something on load (each form is doing something else), and I need to open them all on App load (I'm not showing them, just create instance):

<Code=c#>

form1 frmOne = new form1;
frmOne.LoadData;

form2 frmTwo = new form2;
frmTwo.LoadData;

</Code>

I want to load all of them at the same time (each in seperate thread).
How can I do this



Windows Forms23  
 
 
JRQ





PostPosted: Windows Forms General, Open winforms with Async threads Top

Create a generic method that will create and show your form passing the name of the form for the switch statement. Use the Thread class to run the method as a separate thread.

 
 
Peter Ritchie





PostPosted: Windows Forms General, Open winforms with Async threads Top

You should not create forms on any other thread than the main thread. Windows does not have the concept of a GUI on more than the main thread. The main thread is the only thread that supports a GUI message pump and it only supports one GUI message pump per application.

In addition, only the GUI thread should be doing anything with Form object attributes, You can perform background processing in background threads, but all accesses to Form objects from background threads must be marshaled to the main thread (InvokeRequired/BeginInvoke is the easiest method of doing this).



 
 
TMF





PostPosted: Windows Forms General, Open winforms with Async threads Top

So what can I do

I still need to create 13 forms objects (that connect DB and retrieve data for UI use) on load. You say I can't use threads in winforms at all


 
 
Peter Ritchie





PostPosted: Windows Forms General, Open winforms with Async threads Top

You'll have to create each Form, one-by-one, on the main thread. You can get fooled into thinking you can do what you want with multiple threads; but, you'll run into a brick wall at some point with the limitations that Windows has that manifests as weird, rarely reproducible, problems that will only go away with a redesign. It's best to get the design right as quickly as possible...

Keep in mind, splitting processing across many threads doesn't buy you anything unless you have one processor/core per thread. When the OS has to context switch from thread to thread on the same processor you're actually performing much more work and often will be quantitatively longer.

Have a look at some the articles about improving application load times, they may have something useful:

As well, Chapter 5 of Improving .NET Application Performance and Scalability (Improving Managed Code Performance) might have some useful guidance.



 
 
TMF





PostPosted: Windows Forms General, Open winforms with Async threads Top

OK, I added splash screen open it and load the forms while it is open. after the forms load (I load them in seperate method one by one) I close the splash screen.

Thanks for your help Peter.


 
 
LearnToRock





PostPosted: Windows Forms General, Open winforms with Async threads Top

The splash screen will be the main UI when being displayed. Your real forms will only be loaded after the splash is gone. It's still sync. call. I don't see any advantage of using splash because your forms cannot be loaded from background per Peter's explanataion. Correct me if I'm wrong.

Thanks,
Alan


 
 
cablehead





PostPosted: Windows Forms General, Open winforms with Async threads Top

They can be loaded...they will be loaded on the first (splash) thread. The same UI pump.

It's not a good idea to try to load them in seperate threads.