0

I have created a System Tray Application using Windows Forms Template (Visual C++) in Visual Studio 2008. I have used ContextMenuStrip and NotifyIcon. It's a managed code as I have used the form and Drag/Drop.

I want as soon as this System Tray Application starts, it starts polling for any new USB devices (from a specific vendor) connected.

The logic is ready except I don't know "Where to put this while(1) loop?"

It works fine in a console app that I made but now we want it to be integrated to the system tray app.

Here is the code snippet:

        int numDevices, n = 0;
                while(1)
        {

            Sleep(5000);
            numDevices = usb_find_devices();
            if(connectedDevices > numDevices)
            {
                enumDevices();
                                    connectedDevices++;

            }
        }

It would really be appreciable if anyone could suggest me some pointers on how to proceed.

deepak_
  • 1
  • 2
  • You'll need a thread to prevent your notify icon from being completely dead. Not the proper way to do it, listen for WM_DEVICECHANGE messages instead. – Hans Passant Jul 05 '13 at 14:27
  • possible duplicate of [How to receive the Windows messages without a windows form](http://stackoverflow.com/questions/2061167/how-to-receive-the-windows-messages-without-a-windows-form) – Hans Passant Jul 05 '13 at 14:28

1 Answers1

0

Thank you Hans! I added a new "Component Class" with WM_DEVICECHANGE and it is working fine.

Just in case anyone needs this info: If a function needs to be called as soon as the Windows Forms App starts (Systray app in my case), the respective function can be called after the call to "InitializeComponent()" function. Though it is clearly mentioned "TODO: Add the constructor code here", still a beginner (like me) has inhibitions regarding "Where to put this Function Call??" Hope this helps somebody..

deepak_
  • 1
  • 2