INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Timeouts in a thread?

Timeouts in a thread?

(OP)
Hi All,

I have a borland c++ builder application where I am lauching a thread to manage USB HID communications with an external device. I want to wait a specified amount of time at a point within this thread for the device to be attached. Is there an easy way of setting up a timeout to do this? I could have a for loop with a counter and use Sleep() routine but not sure if this is the best way of doing it.

I apologize in advance for the lack of information but not really sure what extra info to provide. Please ask if neccessary.

RE: Timeouts in a thread?

If you want to suspend unconditionally, the Sleep function (w/o any loop counters) is the best way to go (it's exactly what this function is intendened for).

For more advanced HID programming stuff look at
http://www.lvr.com/hidpage.htm

RE: Timeouts in a thread?

I have not tried anything like this myself before, but have you tried creating a timer object and then using WaitForEvent? Like I said, I have not done this so I do not have any sample code, but I would look at Windows System calls of CreateWaitableTimer, SetWaitableTimer and OpenWaitableTimer, as well as WaitForSingleObject. Alternatively, you could investigate CreateEvent, and set the timeout parameter of the WaitForSingleObject. Just a couple of approaches to try.

RE: Timeouts in a thread?

(OP)
Thanks for the responses. Here's a more detailed description of what I am trying to do:
The particular part in the code is attempting to put the USB HID device into a boot loader mode by sending a USB command. Once the device receives this command I know it will disconnect and reconnect from the USB as it enters the bootloader mode. I have a rough idea of how long this will take (something in the order of 300ms). I have registered for device events in my application so I know when a USB device is attached/detached so what I want is a loop that continuously poles a flag for the device to be attached for a maximum time of (say 500ms).

I think the easiest way would be to do something like this:

CODE --> c++

for(timout = 50; ((timeout > 0) && !DeviceAttached); timeout--)
    sleep(10);    //suspend for 10ms

if(!DeviceAttached)
    return -1;    //error

//check this is the correct device (PID, VID) 

Not sure how long do get the thread to sleep for between checking the DeviceAttached flag though. I think 10ms should be fine.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close