• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 215
  • Last Modified:

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

Jack
0
Actia
Asked:
Actia
  • 2
2 Solutions
 
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...

Regards
Pierre
0
 
Pierre CorneliusCommented:
unit Unit1;

interface

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

type
  TYourThread = class(TThread)
  protected
    procedure Execute; override;
  end;

  TForm1 = class(TForm)
  private
    procedure DoYourHardwareCheck;
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

{ TYourThread }

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

{ TForm1 }

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

end.
0
 
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.
0
 
Slick812Commented:
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
    { Private declarations }
    ValidResp: Boolean; // if true this will end the repeat loop


function DoUserMsgs: Boolean;
var
aMsg: TMSG;
begin
// 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
     begin
     Result := True;
// IMPORTANT, must get WM_QUIT and repost it, set Result so repeat loop is ended
     PostQuitMessage(0);
     Break;
     end else
     if not IsDialogMessage(GetActiveWindow, aMsg) then
       begin
       TranslateMessage(aMsg);
       DispatchMessage(aMsg);
       end;
end;


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

ValidResp := False;
Start1 := GetTickCount;
Timer2.Interval := 300;
Timer2.Enabled := True;
repeat
  if DoUserMsgs or ValidResp then Break;
  WaitMessage;
  { 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');
end;

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

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

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now