开发者

c# how to thread a windows form

开发者 https://www.devze.com 2023-02-25 16:22 出处:网络
I have a method that creates a bound with a windows form and also sents to that window form a parameter. I would like to create a thread to that window form. How can I do it?

I have a method that creates a bound with a windows form and also sents to that window form a parameter. I would like to create a thread to that window form. How can I do it?

public Send_to_windowForm(string name_form, string value_for_labellBox)
{
thread t = new thread(new threadStart (form(value_for_labelBox)).Start();
 Application.Run(new form(value_for_labelBox)); 
form.text=name_form;
}

it's not working. I don't think it's correct. How can I do it? And one more question: how can i handleall the form application that the thread opens? I would like to handle all the fo开发者_开发百科rms because in a specific moment i want to add a new value to one of the forms opened on the desktop.

THX


You need to do something like this:

namespace UIThreadMarshalling {
    static class Program {
        [STAThread]
        static void Main() {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                var tt = new ThreadTest();
                ThreadStart ts = new ThreadStart(tt.StartUiThread);
                Thread t = new Thread(ts);
                t.Name = "UI Thread";
                t.Start();
                Thread.Sleep(new TimeSpan(0, 0, 10));
        }

    }

 public class ThreadTest {
        Form _form;
        public ThreadTest() {
        }

     public void StartUiThread()
     {
        using (Form1 _form = new Form1())
        {
            Application.Run(_form);
        }
     }
  }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消