线程和任务

本文关键字:任务 线程 | 更新日期: 2023-09-27 18:27:02

我试图用线程和任务打开新的窗口窗体,但这段代码打开了新的窗口,但没有在上面显示按钮、文本字段等(窗体正在加载)

private void newFomrm_Click(object sender, EventArgs e)
{
    NewForm a = new NewForm(param); // my Form
    Action showMethod = () => {  a.Show(); };
    Task t1 = new Task(showMethod);
    Thread t = new Thread(new ThreadStart(t1.Start));
    t.Start(); 
}

线程和任务

private void button1_Click(object sender, EventArgs e)
    {
        NewForm a = new NewForm(); // my Form
        Action showMethod = () => {
            Invoke(new Action(() => a.Show())); 
        };
        Task t1 = new Task(showMethod);
        Thread t = new Thread(new ThreadStart(t1.Start));
        t.Start(); 
    }

试试这个。。。您必须将UI操作放在主线程上