【发布时间】: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