c# - 等待从弹出表单中获取信息

标签 c# multithreading forms wait

在一个窗体(主窗体)初始化的时候,会调用另一个窗体得到一堆起始输入,然后传递很多信息:

Form3 getup = new Form3();
getup.Show();
example = getup.example;

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

Form3 getup = new Form3();
getup.Show();
waitfordone();
example = getup.example;

ATM,我试过使用 while 语句:

Form3 getup = new Form3();
getup.Show();
While(getup.visible=true)Console.WriteLine("waiting");
example = getup.example;

但这会导致挂起...也就是说,它运行,然后卡住。我怀疑这是因为 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;

但这也会导致它挂起。也许出于同样的原因。我调查了自动重置事件。

我试过:

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

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

谁能解释一下如何做到这一点,阅读只会让我更加困惑。

最佳答案

您可能需要的是一个对话框。

Form3 getup = new Form3();
getup.ShowDialog();
example = getup.example;

这将暂停执行,只有在表单关闭后才会继续。

关于c# - 等待从弹出表单中获取信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12007541/

相关文章:

c# - 在C#或Java中实现大规模并行计算引擎的线程替代方法?

C# Selenium : Click button under iframe

Java 并发数 : how to retrieve data from several thousands TCP sockets at once and synchronously?

forms - 在 Prototype 中使用表单验证的 HTML5 表单占位符回退

java - 如何在 Java 代码中制作输入表单(不是使用 JForm 的 Netbeans)?

java - html 到 jsp 电子邮件表单 post - 空参数

c# - 使用 Entity Framework 向现有数据库添加或更新数据

c# - 如何在angularjs中使用ng-repeat渲染mathml?

c++ - TBB 管道输出不正确

java - Swing 对话框如何工作?