Click here to Skip to main content
15,170,343 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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;
                        }
                    }
                }
            }
        }


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!!!!!
Posted

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
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900