locked
Form in new thread RRS feed

  • Question

  • Hi,

     

    I would like to open a win form in new thread and keep it open.

     

    But the problem is that when I start new thread the form is shown for a while and then it is being closed.

     

    Do you know how can I keep it open?

     

    the code looks as follow:

     

    public void OpenForm()

    {

    Thread t = new Thread(new ThreadStart(A));

    t.SetApartmentState(ApartmentState.STA);

    t.Start();

    }

    public void A()

    {

    try

    {

    Form f = new Form();

    f.Show();

    }

    catch (Exception ex) { HandleError(ex); }

    finally { }

     

    Thank you for the answers,

    Łukasz Magdziarz

    Friday, May 11, 2007 1:33 PM

Answers

  •  

    Hi Łukasz Magdziarz,

     

    Try

    Code Snippet

     

    public partial class whiteline_statusstrips : Form

        {

            public whiteline_statusstrips()

            {

                InitializeComponent();

            }

     

            [DllImport("User32.dll", EntryPoint = "FindWindow")]

            private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

            [DllImport("User32.dll", EntryPoint = "SetParent")]

            private static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);

     

            IntPtr hDeskTop = FindWindow("Progman", Application.ProductName);

            delegate void open(Form p);

     

            public void OpenForm()

            {

                Thread t = new Thread(new ThreadStart(A));

                t.Start();

            }

     

            public void A()

            {

                openMethod(this);

            }

     

            private void openMethod(Form p)

            {

                if (p.InvokeRequired)

                {

                    open d = new open(openMethod);

                    this.Invoke(d, new object[] { p });

                }

                else

                {

                    Form m = new Form();

                    m.Show();

                    m.Visible = true;

                    SetParent(m.Handle, hDeskTop);

                }

            }

     

            private void button1_Click(object sender, EventArgs e)

            {

                OpenForm();

            }

        }

     

    Hope it helps.

     

    Regards

     

    Friday, May 18, 2007 7:08 AM

All replies

  • Do you really need a new thread?  Usually you let all the UI stuff happen in the same thread, and then create other threads to do any time-consuming work.  Much easier.

    - Rory
    Friday, May 11, 2007 2:45 PM
  • Thanks for your answer.

     

    I am doing it this way to workaround exception which occurs when I try to load the form via COM+ service.

     

     ************* Exception Text **************
    System.InvalidOperationException: DragDrop registration did not succeed. ---> System.Threading.ThreadStateException: Current thread must be set to single thread apartment (STA) mode before OLE calls can be made. Ensure that your Main function has STAThreadAttribute marked on it.
       at System.Windows.Forms.Control.SetAcceptDrops(Boolean accept)
       --- End of inner exception stack trace ---
       at System.Windows.Forms.Control.SetAcceptDrops(Boolean accept)
       at System.Windows.Forms.Control.OnHandleCreated(EventArgs e)
       at System.Windows.Forms.Form.OnHandleCreated(EventArgs e)
       at System.Windows.Forms.Control.WmCreate(Message& m)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
       at System.Windows.Forms.ContainerControl.WndProc(Message& m)
       at System.Windows.Forms.Form.WmCreate(Message& m)
       at System.Windows.Forms.Form.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

    Friday, May 11, 2007 3:04 PM
  •  

    Hi Łukasz Magdziarz,

     

    Try

    Code Snippet

     

    public partial class whiteline_statusstrips : Form

        {

            public whiteline_statusstrips()

            {

                InitializeComponent();

            }

     

            [DllImport("User32.dll", EntryPoint = "FindWindow")]

            private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

            [DllImport("User32.dll", EntryPoint = "SetParent")]

            private static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);

     

            IntPtr hDeskTop = FindWindow("Progman", Application.ProductName);

            delegate void open(Form p);

     

            public void OpenForm()

            {

                Thread t = new Thread(new ThreadStart(A));

                t.Start();

            }

     

            public void A()

            {

                openMethod(this);

            }

     

            private void openMethod(Form p)

            {

                if (p.InvokeRequired)

                {

                    open d = new open(openMethod);

                    this.Invoke(d, new object[] { p });

                }

                else

                {

                    Form m = new Form();

                    m.Show();

                    m.Visible = true;

                    SetParent(m.Handle, hDeskTop);

                }

            }

     

            private void button1_Click(object sender, EventArgs e)

            {

                OpenForm();

            }

        }

     

    Hope it helps.

     

    Regards

     

    Friday, May 18, 2007 7:08 AM