Send command -> wait max 3 seconds -> send command

Posted on 2006-03-29
Last Modified: 2010-04-05
Hello :-)

My application should:

1. send command to external hardware
2. try to get the response for the max 3 seconds
3. work with response
1. send another command to external hardware

The problem is that I do not really know how to stop the execution of the main program thread- make it wait for a response for max 3 seconds and keep the application responsive.

I think there are two possible solutions:

- simple one- like opening window as modal and stop the main thread execution in this way
- the one with threads- send command as a thread and make the main thread wait until the thread finishes execution

Could you please help me with:

- simple solution (without threads)
- more advanced (with threads)

Thank you very much

Question by:Actia
    LVL 14

    Expert Comment

    by:Pierre Cornelius
    without threads it is easy:

      //1. send command to external hardware
      Sleep(3000); //2. try to get the response for the max 3 seconds
      //3. work with response
      //1. send another command to external hardware

    demo with thread to follow...

    LVL 14

    Assisted Solution

    by:Pierre Cornelius
    unit Unit1;


      Windows, Messages, SysUtils, Variants, Classes, Forms;

      TYourThread = class(TThread)
        procedure Execute; override;

      TForm1 = class(TForm)
        procedure DoYourHardwareCheck;

      Form1: TForm1;


    {$R *.dfm}

    { TYourThread }

    procedure TYourThread.Execute;
      //1. send command to external hardware
      //2. try to get the response for the max 3 seconds
      //3. work with response

    { TForm1 }

    procedure TForm1.DoYourHardwareCheck;
    var t: TYourThread;
      t:= TYourThread.Create(true);
      t.FreeOnTerminate:= true;

    LVL 11

    Expert Comment

    I'm not sure if "simple sollution" is possible. In some cases it's possible that thread will freeze while executing 1st point (if it waits for response). In those cases you may have to use 2nd sollution with threads.
    LVL 33

    Accepted Solution

    hello Actia, , I hope I understand what you want to do, as for "simple", , maybe not so much?
    Anyway, here is some code that will "Block" code progression at the repeat loop, this still allows the thread to process all system messages.
    However, even though the "code progression" is blocked, all of your form's "Message" events will still be able to execute, so ALL buttons will still work, and if you click a button (or menu) then the program will execute that code, even though the code progression is blocked.
    So You may need to disable some buttons?, , you were not  clear (to me) about what you may mean by "stop the execution of the main program thread". .
    Below is some code that will stop code progression at the repeat loop, the WaitMessage will " keep the application responsive", but it is NOT Modal, since all buttons and controls are still active.
    I added a TTimer, to keep a timed query of the  GetWindowLong(  ), not sure about your "send command to external hardware" method, GetWindowLong(  ) is what I used here.

        { Private declarations }
        ValidResp: Boolean; // if true this will end the repeat loop

    function DoUserMsgs: Boolean;
    aMsg: TMSG;
    // for the WaitMessage repeat loop , you will need to
    //get the message queue messages and dispatch them
    Result := False; // repeat loop continues as long as Result is False
    while PeekMessage(aMsg, 0, 0, 0, PM_REMOVE) do
      if aMsg.message = WM_QUIT then
         Result := True;
    // IMPORTANT, must get WM_QUIT and repost it, set Result so repeat loop is ended
         end else
         if not IsDialogMessage(GetActiveWindow, aMsg) then

    procedure TForm1.sbut_DoWaitMsgClick(Sender: TObject);
    ReH: THandle;
    Start1: Cardinal;
    ReH := GetWindowLong(Handle, GWL_HWNDPARENT);
    if ReH = 0 then
      ShowMessage('Success, Got Responce');

    ValidResp := False;
    Start1 := GetTickCount;
    Timer2.Interval := 300;
    Timer2.Enabled := True;
      if DoUserMsgs or ValidResp then Break;
      { the  WaitMessage  function is what allows this to be "Modal Wait" AND
        keep the thread from progressing in code past this point. The WaitMessage
        function will yield control to other threads when this thread has no messages}
      until ValidResp or (GetTickCount-Start1 > 6000);
    Timer2.Enabled := False;
    ShowMessage('End of Wait');

    procedure TForm1.Timer2Timer(Sender: TObject);
    if GetWindowLong(Handle, GWL_HWNDPARENT) = 0 then
      ShowMessage('Success, Got Responce');
      ValidResp := True;
      Timer2.Enabled := False;

     = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
    I hope this give you some ideas for this.
    ask questions if you need more information

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Suggested Solutions

    Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
    In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    24 Experts available now in Live!

    Get 1:1 Help Now