Forms created in a separate thread are shown in the background

S

Sunil Menon

Dear All,
I am trying to create multiple forms using thread...when one of the
external application requests for a Form my MainThread creates the
Form in a new thread...the form appears in the ForeGround if it has
been created for the first time...but subsequent creations are created
behind the application that invokes it...if I remove creation of Form
in a thread it works fine...Wot is going wrong here...
Here is the code snippet...
Thread oFormThread = new Thread(new ThreadStart(OpenFormInThread));
oFormThread.ApartmentState = ApartmentState.STA;
oFormThread.IsBackground = false;

[STAThread]
public void OpenFormInThread()
{
Form ofrm = (Form)Activator.CreateInstance(typ);
ofrm.ShowDialog();
if(ofrm != null)
ofrm.Close();
}

In the Form's Load I have written...
ShowWindow(this.Handle , SW_RESTORE);
SetForegroundWindow(this.Handle );
this.BringToFront();

But this does not seem to work...am I doing something wrong here?
Please help...

TALIA

Many Regards
Jack
 
H

Herfried K. Wagner [MVP]

* (e-mail address removed) (Sunil Menon) scripsit:
I am trying to create multiple forms using thread...when one of the
external application requests for a Form my MainThread creates the
Form in a new thread...the form appears in the ForeGround if it has

Create all forms in the app's main UI thread and use invoking to access
them from within another thread:

Multithreading:

<URL:http://msdn.microsoft.com/library/en-us/dnforms/html/winforms06112002.asp>
<URL:http://msdn.microsoft.com/library/en-us/dnforms/html/winforms08162002.asp>
<URL:http://msdn.microsoft.com/library/en-us/dnforms/html/winforms01232003.asp>

<URL:http://www.devx.com/dotnet/Article/11358/>

<URL:http://msdn.microsoft.com/library/e...SystemWindowsFormsControlClassInvokeTopic.asp>

Multithreading in Visual Basic .NET (Visual Basic Language Concepts)
<URL:http://msdn.microsoft.com/library/en-us/vbcn7/html/vaconthreadinginvisualbasic.asp>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top