Question

How do I check if another application is busy?

I have a program that sends text to a console. The text that I will send contains #13 char (e.g. ls#13cd documents#13dir). In other words I want to send many commands at one time and the console will process them one by one. I am sending the text character by character. Sometimes the console only executes ls and cd documents. I think maybe this is because my program continuously sends character even if the console is busy, in which case the console does not receive incoming characters.

This is my code:

procedure TForm1.SendTextToAppO(Str: String; AHandle: Integer);
var
  iWindow, iPoint, i: Integer;
  SPass: PChar;
  sList: TStringList;
begin
sList := TStringList.Create;
  ExtractStrings([#13],[' '],PChar(Str),sList);
  iWindow := AHandle;// AHandle is the handle of the console
  iPoint := ChildWindowFromPoint(iWindow, Point(50,50));
  for i:=0 to sList.Count-1 do begin
    SPass := PChar(sList[i]);
    try
      while(SPass^ <> #$00) do begin
      SendMessage(iPoint,WM_CHAR,Ord(SPass^),0);
      Inc(SPass);
      end;
      SendMessage(iPoint,WM_KEYDOWN,VK_RETURN,0);
    except
        // do nothing;
    end;
  end;
end;

I am using Delphi 7.

Was it helpful?

Solution

If I interpret you question correctly you are sending the text to some sort of shell/command line interpreter and you want it to execute your commands.

Usually command line interpreters output a certain prompt (like $ on a Linux system or C:\ for DOS) that indicate that they can accept new commands. You need to read the output to wait for the appropriate prompt before you send another command. If you don't your sent text will be consumed as input by the currently running command (like you experienced).

OTHER TIPS

lothar is on the right track; what you want to do is, instead of using ShellExecute, use CreateProcess. Look around Stack Overflow and Google for "Console Redirection" - that'll get you what you're looking for.

I think I understand what's going on, not that I have a fix for it:

You send a command to the console. While the command is running that program will receive the keys you send.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top