How to wait for hardware to appear Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
denarced
Knows some wx things
Knows some wx things
Posts: 47
Joined: Sun Dec 04, 2011 7:25 am

How to wait for hardware to appear

Post by denarced » Wed Feb 01, 2012 11:46 am

Hello,

I have an operation in which I'm waiting for a USB stick to be plugged into the computer.
The question is how to wait for it.

Background
Now there's a modeless dialog BatchDialog (wxDialog) with two buttons: next and abort.
The BatchDialog has an observer (wiki), BatchDialogObserver.
The observer is notified when the user presses next button by calling its nextClicked method.

Problem
The nextClicked method attempts to connect to the USB stick and if it fails, wxTimer is started.
The timer calls nextClicked again after 1000 milliseconds (1 second).
The timer is used in order to keep abort button alive and the GUI in general.
What should be the delay? 100ms?
Is this (polling) the best strategy? Is there some better way?

The idea for using wxTimer was suggested in another thread (Re: How to implement timeout behavior for wxTextCtrl)

User avatar
doublemax
Moderator
Moderator
Posts: 15183
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: How to wait for hardware to appear

Post by doublemax » Wed Feb 01, 2012 4:35 pm

Do you need a cross-platform solution?

If it was MSW only, i'd catch the WM_DEVICECHANGE message and check for the USB device from there:
http://forums.wxwidgets.org/viewtopic.php?f=23&t=22811

In general i think i would solve it slightly differently:
I'd disable the "next" button, have a 1 sec timer, check for the USB device in the timer event, and if it's present, enable the button.
Use the source, Luke!

Jessy V
Earned a small fee
Earned a small fee
Posts: 17
Joined: Sat Feb 18, 2012 9:12 pm

Re: How to wait for hardware to appear

Post by Jessy V » Tue Feb 21, 2012 5:44 pm

The solution for this is very platform-dependant.
So please specify the platform you're targetting.

denarced
Knows some wx things
Knows some wx things
Posts: 47
Joined: Sun Dec 04, 2011 7:25 am

Re: How to wait for hardware to appear

Post by denarced » Sat Mar 03, 2012 2:56 pm

doublemax wrote:Do you need a cross-platform solution?

If it was MSW only, i'd catch the WM_DEVICECHANGE message and check for the USB device from there:
http://forums.wxwidgets.org/viewtopic.php?f=23&t=22811

In general i think i would solve it slightly differently:
I'd disable the "next" button, have a 1 sec timer, check for the USB device in the timer event, and if it's present, enable the button.
Finally got around to replying this. I don't need a cross-platform solution but I went with your
second more general solution. The first one relating to the WM_DEVICECHANGE message appears
a lot better and I'll see if I can one day replace the current polling solution with it.

Post Reply