【问题标题】:In C# how to show a GUI from another GUI, based on the event from a class in another thread?在 C# 中,如何根据来自另一个线程中的类的事件显示来自另一个 GUI 的 GUI?
【发布时间】:2010-04-14 11:14:30
【问题描述】:

我正在开发基于 DirectX 的模拟器。 我必须在其上检查设备是否已插入或从 PC 中移除。

我已经设法在另一个线程上为设备到达和移除创建类,这会从线程本身引发一个事件设备到达或移除。 相应的事件方法正在主窗体中被调用:

假设Form1 是主窗口,Form2 是辅助窗口。

Form2 form2Instance = new Form2();

我想显示另一个表单 (Form2),将主窗口 (Form1) 保留在后面(在一般情况下,它的行为与 form2Instance.ShowDialog(); 相同。) 经过几次尝试,我已经做到了

Applicatin.Run(new Form2());,但 Form2 在任何方面都不像'form2Instance.ShowDialog();

如果可以帮助回答,请提供代码:

iARMdetectionThreadClass detection;
InProgram_iARMdetection iARMStatusGUI;

 private void Form2_Load(object sender, EventArgs e)
        {
         iARMStatusGUI = new InProgram_iARMdetection();
         detection = new iARMdetectionThreadClass();
         detection.IniARM_device_Arrive += new iARMdetectionThreadClass.iARM_device_ArrivedEventHandler(detection_IniARM_device_Arrive);
         detection.IniARM_device_Remove += new iARMdetectionThreadClass.iARM_device_RemovedEventHandler(detection_IniARM_device_Remove);
         detection.startThread();
        }


 void detection_IniARM_device_Remove(iARM_deviceInfo senderInfo)
        {

            detection.StopCheckBeingRemoved();
            MethodInvoker act = delegate
            {
                this.label_iARMStatus.Text = detection.iARM_deviceInf.iARMStatus;
            };
            this.label_iARMStatus.BeginInvoke(act);

            Application.Run(new InProgram_iARMdetection()); //Blocking code

            detection.StartCheckBeingRemoved();

        }

 void detection_IniARM_device_Arrive(iARM_deviceInfo senderInfo)
        {
            MethodInvoker act = delegate
            {
                this.label_iARMStatus.Text = detection.iARM_deviceInf.iARMStatus;
            };
            this.label_iARMStatus.BeginInvoke(act);
            //detection.StopCheckArriving();
            //detection.StartCheckArriving();
        }

我需要代码是阻塞代码。在这里:

Application.Run(new InProgram_iARMdetection()); //Blocking code

【问题讨论】:

  • 如果设备再次插入 PC,出现的对话框必须自行消失。哪个表现不错,但在主窗口后面。
  • 显然,如果您希望 form2 显示为对话框,您必须使用 ShowDialog() 方法。为什么在事件中使用 Application.Run 而不是 ShowDialog()?
  • @Aseem Gautam:Bcos 当我使用 form2Instance.ShowDialog() 时,它第一次显示,之后它也消失了,但在下一次删除事件时效果不佳。不知道为什么。所以 Application.Run(new Form2());

标签: c# .net multithreading user-interface dialog


【解决方案1】:

也许mainform.AddOwnedForm(form2) 会做你想做的事。它将使form2 显示在mainform 前面,当其中一个被最小化时,另一个也被最小化。

【讨论】:

  • 是的,或者只是form2.Show(this)
  • 没有按需要工作。 :(如果您需要任何其他参考请提及。
  • 当我尝试 AddOwnedForm() 然后当我调用 .show() 时它显示: System.InvalidOperationException:跨线程操作无效:控制从线程以外的线程访问的“OnProgram_iARMdetection”创建于。在 System.Windows.Forms.Control.get_Handle() 在 System.Windows.Forms.Form.UpdateHandleWithOwner() 在 System.Windows.Forms.Form.set_Owner(Form value) 在 System.Windows.Forms.Form.AddOwnedForm(Form ownForm) OnProgram_iARMdetection 已在主窗体本身中实例化,并且 addOwnedForm() 和 Show() fns 都从主窗体本身调用。
【解决方案2】:

您应该在 form2 而不是 form1 上处理 remove 事件,并使用 ShowDialog() 方法。

因此,当在 form1 上触发到达事件时,它会像对话框一样打开 form2。现在,当设备被拔出时,会触发 form2 移除事件,您可以在其中关闭表单。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多