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

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

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Pierre CorneliusCommented:
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...

Pierre CorneliusCommented:
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;

ZhaawZSoftware DeveloperCommented:
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.
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.