views:

259

answers:

3

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 (i.e. ls#13cd documents#13dir). In other words I want to send many commands at one time and the console will process it one by one. I am sending it character by character. But sometimes it only executes ls and cd documents. I think maybe this is because it continuously sends character even if the console is still busy and for that, it didn't receive the incoming character. 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;

Please let me know if you did not understand my question. By the way, I am using Delphi 7.

+4  A: 

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).

lothar
yes, that is what I am thinking, but how? Do you mean after sending a character, I will read the output of the console? I don't think it's ideal. I have read about WaitforInputIdle.But I am using shellexecute to open the console. So it won't work for me.
junmats
lothar, I tried your suggestion but the only text that I can copy are those that I have inputted. I can't copy the predefined text(i.e c:\). Do you know of such that will copy all the text inside the console? Your help will be much appreciated.
junmats
I think overslacked is right. You need to start the command interpreter as a process where you can read and write the input and output via e.g. a pipe. That would be much easier.
lothar
A: 

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.

Loren Pechtel
+3  A: 

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.

overslacked