how create an instance of a windows form from a thread

Posted on 2004-11-09
Last Modified: 2010-04-15
hi i have the following problem
i have a Form1 (main form ) which have this code on a button

Asyn newSession = new Asyn(txtUserName.Text,txtPassword.Text.Trim(),this);
               Thread sessionThread=new Thread(new ThreadStart(newSession.StartSession));

public class AsynSock : ISocketEventListener

public Asyn(string user,string pass,Form1 l_form)
public void StartSession()
                         Form2 frm=new frmMain();

 the form shows but it hangs and gives not responding .
i couldn't figure where's the problem could anyone help me.
Question by:hema3i
    1 Comment
    LVL 11

    Accepted Solution

    I think reason for this problem is u r not creating new form on STA UI thread

    just try this

    private void btnCreateForm_Click(object sender, System.EventArgs e)
                      Thread sessionThread=new Thread(new ThreadStart(StartSession));


                delegate void CallCreateForm();
                private void StartSession()
                                // this is refrence to form1
                      this.Invoke(  new CallCreateForm(this.CreateMainForm));

                private void CreateMainForm()
                      Form2 frm = new Form2 ();

    Hope this will help

    Featured Post

    Courses: Start Training Online With Pros, Today

    Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

    Join & Write a Comment

    Article by: Ivo
    C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
    Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now