Thread: sleep for data in a wxwidget application

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    35

    sleep for data in a wxwidget application

    How I can wait some data from serial port with a timeout without lock the events in my application? I tried wxMilliSleep() function but it lock my application and do not handle events, i.e. button click for stop the operation.
    I can fragment my timeouts and every atomic timeouts I could "dispatch" the events...but how I can do it?

    Thx
    Lollo

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Start a timer and return to the message loop. The timer should send a message to the loop when it runs out, so you can cancel the operation. Retest whether you received data every now and then while it runs.

    If that's not possible, you'll have to create a background thread. That makes things a lot more complicated.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    35
    what do you mean with "message loop"?
    For messages you mean the OS messages?

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    At a basic level, event-driven applications like modern GUI applications sit in some loop, waiting for messages (events) to arrive. That's the even loop. Most toolkits hide this loop, but I learned Win32 API first, so I'm used to the term.

    Returning to the message loop means simply going back to waiting for more events.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by CornedBee View Post
    Start a timer and return to the message loop. The timer should send a message to the loop when it runs out, so you can cancel the operation. Retest whether you received data every now and then while it runs.
    This might sound like overkill, but... A program could be written which handles the reading/writing to the serial port, and which exposes a UNIX socket interface to clients. Only a single client can connect at a time. Clients connect and use send()/recv(), and the serial daemon translates this to serial IO.

    Now that the interface is a plain old socket you can plug it into the event loop of your application just like any other socket.

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    The serial port should be a file descriptor, so you can do async reads on that, too.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with com application
    By amardon in forum C++ Programming
    Replies: 3
    Last Post: 10-06-2005, 05:50 AM
  2. MFC run application by clicking on file...
    By dug in forum Windows Programming
    Replies: 4
    Last Post: 12-02-2004, 04:33 AM
  3. Problem with Sleep() #$@^#$%^
    By intruder in forum C++ Programming
    Replies: 8
    Last Post: 10-11-2004, 06:46 AM
  4. Sleep is overrated...
    By Polymorphic OOP in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 01-24-2003, 12:40 PM