在不同方法中的按钮结束 运行 方法
On button end running methods in different method
所以我有一些代码可以检查 USB 设备的插件和移除。
public void Main_Shown(object sender, EventArgs e)
{
var watcheradd = new ManagementEventWatcher();
var queryadd = new WqlEventQuery("SELECT * FROM Win32_DeviceChangeEvent WHERE EventType = 2");
watcheradd.EventArrived += new EventArrivedEventHandler(watcher_deviceadded);
watcheradd.Query = queryadd;
watcheradd.Start();
var watcherremove = new ManagementEventWatcher();
var queryremove = new WqlEventQuery("SELECT * FROM Win32_DeviceChangeEvent WHERE EventType = 3");
watcherremove.EventArrived += new EventArrivedEventHandler(watcher_deviceremoved);
watcherremove.Query = queryremove;
watcherremove.Start();
}
我想知道我是否可以在我的程序退出之前停止 watchremove
和 watcheradd
循环以防止由以下停止代码引起的异常:
private void Exit_Main(object sender, EventArgs e)
{
Process p = new System.Diagnostics.Process();
ProcessStartInfo si = new System.Diagnostics.ProcessStartInfo();
si.RedirectStandardInput = false;
si.CreateNoWindow = true;
si.UseShellExecute = false;
si.FileName = "adb.exe";
si.Arguments = "kill-server";
p = Process.Start(si);
p.WaitForExit();
Application.Exit();
//EXIT APPLICATION!!!
}
as Main_Shown.deviceadd.Stop();
不是正确的语法或执行此操作的方法。
结果应该安全地停止上面的循环,然后让程序无异常退出。
找到解决办法了。
所以我需要添加
ManagementEventWatcher watcheradd = new ManagementEventWatcher();
WqlEventQuery queryadd = new WqlEventQuery();
ManagementEventWatcher watcherremove = new ManagementEventWatcher();
WqlEventQuery queryremove = new WqlEventQuery();
在 class 之上,并将 classes 设置为全局。所以现在它可以使用下面的代码毫无问题地退出。
`si.FileName = "adb.exe";
si.Arguments = "kill-server";
p = Process.Start(si);
p.WaitForExit();
watcherremove.Stop();
watcheradd.Stop();
Application.Exit();
//EXIT APPLICATION!!`
感谢大家的帮助。
所以我有一些代码可以检查 USB 设备的插件和移除。
public void Main_Shown(object sender, EventArgs e)
{
var watcheradd = new ManagementEventWatcher();
var queryadd = new WqlEventQuery("SELECT * FROM Win32_DeviceChangeEvent WHERE EventType = 2");
watcheradd.EventArrived += new EventArrivedEventHandler(watcher_deviceadded);
watcheradd.Query = queryadd;
watcheradd.Start();
var watcherremove = new ManagementEventWatcher();
var queryremove = new WqlEventQuery("SELECT * FROM Win32_DeviceChangeEvent WHERE EventType = 3");
watcherremove.EventArrived += new EventArrivedEventHandler(watcher_deviceremoved);
watcherremove.Query = queryremove;
watcherremove.Start();
}
我想知道我是否可以在我的程序退出之前停止 watchremove
和 watcheradd
循环以防止由以下停止代码引起的异常:
private void Exit_Main(object sender, EventArgs e)
{
Process p = new System.Diagnostics.Process();
ProcessStartInfo si = new System.Diagnostics.ProcessStartInfo();
si.RedirectStandardInput = false;
si.CreateNoWindow = true;
si.UseShellExecute = false;
si.FileName = "adb.exe";
si.Arguments = "kill-server";
p = Process.Start(si);
p.WaitForExit();
Application.Exit();
//EXIT APPLICATION!!!
}
as Main_Shown.deviceadd.Stop();
不是正确的语法或执行此操作的方法。
结果应该安全地停止上面的循环,然后让程序无异常退出。
找到解决办法了。 所以我需要添加
ManagementEventWatcher watcheradd = new ManagementEventWatcher();
WqlEventQuery queryadd = new WqlEventQuery();
ManagementEventWatcher watcherremove = new ManagementEventWatcher();
WqlEventQuery queryremove = new WqlEventQuery();
在 class 之上,并将 classes 设置为全局。所以现在它可以使用下面的代码毫无问题地退出。
`si.FileName = "adb.exe";
si.Arguments = "kill-server";
p = Process.Start(si);
p.WaitForExit();
watcherremove.Stop();
watcheradd.Stop();
Application.Exit();
//EXIT APPLICATION!!`
感谢大家的帮助。