等待从弹出窗体中获取信息

8 c# forms multithreading wait

在初始化表单(主窗体)时,它调用另一个窗体来获取一堆启动输入,然后传输大量信息:

Form3 getup = new Form3();
getup.Show();
example = getup.example;
Run Code Online (Sandbox Code Playgroud)

但是,我需要等待这个新的表单信息完成.

Form3 getup = new Form3();
getup.Show();
waitfordone();
example = getup.example;
Run Code Online (Sandbox Code Playgroud)

ATM,我尝试过使用while语句:

Form3 getup = new Form3();
getup.Show();
While(getup.visible=true)Console.WriteLine("waiting");
example = getup.example;
Run Code Online (Sandbox Code Playgroud)

但是这会导致挂起...也就是说,它会运行,然后冻结.我怀疑这是因为while循环正在吃掉所有的处理.所以,我试着创建一个新线程

Form3 getup = new Form3();
Thread t = new Thread(getup.Show());
t.start();
While(getup.visible=false)Console.WriteLine("waiting"); // takes a little bit to open
While(getup.visible=true)Console.WriteLine("waiting"); //waits for close
example = getup.example;
Run Code Online (Sandbox Code Playgroud)

但这也导致它挂起.也许出于同样的原因.我已经研究过autoresetevents.

我试过了:

AutoResetEvent invisible = new AutoResetEvent(false);
Form3 getup = new Form3();
void setup_invisible(object sender, EventArgs e)
{
    if (getup.Visible == false) invisible.Set();
}
public ... {
getup.VisibilityChanged += new EventHandle(setup_Invisible);
getup.show();
invisible.WaitOne();
... }
// and many other variations on this
Run Code Online (Sandbox Code Playgroud)

但是,它打开form3,关闭它(因为线程已完成?),然后挂起 invisible.WaitOne();

有人可以解释一下如何做到这一点,阅读只会让我更加困惑.

Dav*_*New 11

你可能需要的是一个对话框.

Form3 getup = new Form3();
getup.ShowDialog();
example = getup.example;
Run Code Online (Sandbox Code Playgroud)

这将暂停执行,并仅在表单关闭后继续执行.