获取奇怪的运行时错误 [英] Getting weird run time Error
问题描述
嘿,我创建了一个程序,该程序扫描WMI是否存在USB操作,并触发一个事件,然后从列表中检查它是什么USB事件,完全与应用有关.无论如何,该代码可以完美地工作!
然后,我创建了一个代码,通过创建一个NotifyIcon并将表单默认设置为Minimize来隐藏启动时的表单.触发Minimize事件,然后隐藏该窗体,并出现系统托盘中的图标,双击该事件将带回该窗体……但是,我真正想要的是Usb事件来触发该窗体也重新出现....但是可悲的是我遇到了一些错误,不确定如何解决.
Hey people, so here''s the thing I''ve created a program that scans the WMI for any usb action and is triggers an event I then check from a list what USB event it was, completely applicatino specific. Anyway that code works perfectly!
Then I added code to hide the form on startup, by creating a NotifyIcon, and having the form default to Minimize. The Minimize event triggers and then the form is hidden and the icon in the system tray appears, a double click event on the icon brings back the form...... However, what I actually want is the Usb event to trigger the form re-appearing as well....But sadly I get some error, not sure how to solve it.
<pre lang="cs">public void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
{
this.Show();
this.WindowState = FormWindowState.Normal;
}
private void Form1_Resize(object sender, EventArgs e)
{
notifyIcon1.BalloonTipTitle = "Minimize to Tray App";
notifyIcon1.BalloonTipText = "You have successfully minimized your form.";
if (FormWindowState.Minimized == this.WindowState)
{
notifyIcon1.Visible = true;
notifyIcon1.ShowBalloonTip(500);
this.Hide();
}
else if (FormWindowState.Normal == this.WindowState)
{
notifyIcon1.Visible = false;
}
}
private void Form1_Load(object sender, EventArgs e)
{
this.Hide();
// this.WindowState = FormWindowState.Minimized;
}
WqlEventQuery weqQuery = new WqlEventQuery();
weqQuery.EventClassName = "__InstanceOperationEvent";
weqQuery.WithinInterval = new TimeSpan(0, 0, 3);
weqQuery.Condition = @"TargetInstance ISA ''Win32_PnPEntity''";
m_mewWatcher = new ManagementEventWatcher(weqQuery);
m_mewWatcher.EventArrived += new EventArrivedEventHandler(m_mewWatcher_EventArrived);
m_mewWatcher.Start();<pre>
<pre lang="cs">public void m_mewWatcher_EventArrived(object sender, EventArrivedEventArgs e)
{
bool bUSBEvent = false;
foreach (PropertyData pdData in e.NewEvent.Properties)
{
ManagementBaseObject mbo = (ManagementBaseObject)pdData.Value;
if (mbo != null)
{
foreach (PropertyData pdDataSub in mbo.Properties)
{
if (pdDataSub.Value == null)
{
}
else
{
namez = pdDataSub.Value.ToString();
if ( namez.Contains(" Electronics Dongle"))//&& pdDataSub.Value.ToString() == "USB")
{
position = namez.IndexOf("(COM");
position2 = namez.IndexOf(")");
bUSBEvent = true;
break;
}
}
}
if (bUSBEvent)
{
if (e.NewEvent.ClassPath.ClassName == "__InstanceCreationEvent")
{
if (dongle_found == false) //We only need to come in here if dongle not found either by startup check
{// or via the usb entry
if ((position > 0))
// Add comport Name
if (SP.IsOpen)
{
SP.Close();
}
SP.PortName = namez.Substring(position + 1, position2 - (position + 1));
dongle_found = true;
}
Console.WriteLine("USB was plugged in");
File.AppendAllText(path, "bleh" + " Con");
string appendText = Environment.NewLine;
File.AppendAllText(path, appendText);
try
{
this.Show();
// this.WindowState = FormWindowState.Normal;
// this.Activate();
}
catch (InvalidCastException q)
{
Console.WriteLine("Caught: {0}", q);
}
notifyIcon1.Visible = false;
}
else if (e.NewEvent.ClassPath.ClassName == "__InstanceDeletionEvent")
{
if (SP.IsOpen)
{
SP.Close();
}
Console.WriteLine("USB was plugged out");
File.AppendAllText(path, "bleh" + "DC");
string appendText = Environment.NewLine;
File.AppendAllText(path, appendText);
dongle_found = false;
}
}
}
}
}
问题是当我想重新显示表单时出现的,this.show()以及以下各行引发了运行时错误....
``BackGroundWorkers.vshost.exe''(受管理):已加载``C:\ Windows \ assembly \ GAC_MSIL \ System.Configuration \ 2.0.0.0__b03f5f7f11d50a3a \ System.Configuration.dll'',已跳过加载符号.模块已优化,调试器选项"Just My Code"已启用.
我不知道发生了什么事.帮助!!!!!
The problem comes in when I want to have the form re-appear, this.show() and the lines following throws a run time bug....
''BackGroundWorkers.vshost.exe'' (Managed): Loaded ''C:\Windows\assembly\GAC_MSIL\System.Configuration\2.0.0.0__b03f5f7f11d50a3a\System.Configuration.dll'', Skipped loading symbols. Module is optimized and the debugger option ''Just My Code'' is enabled.
I have no idea what is going on. Help!!!!!
推荐答案
您正试图在另一个线程中显示表单.使用Control.Invoke
阅读下一篇文章以获取更多信息.
http://msdn.microsoft.com/en-us/library/zyzhdc6b.aspx [ ^ ]
You are trying to show the form in another thread. Use Control.Invoke
Read the next article for more information.
http://msdn.microsoft.com/en-us/library/zyzhdc6b.aspx[^]
是的,我以为我在做类似这样的有趣的事情,解决了一个简单的委托程序,解决了整个问题.....谢谢
Yeah thought as much I was doing something funny like that, sorted out the problem a simple delegate procedure sorted out the whole issue.....Thanks
这篇关于获取奇怪的运行时错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!