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.
我有一个程序将文本发送到控制台。我将发送的文本包含#13 char(例如ls#13cd文档#13dir)。换句话说,我想一次发送许多命令,控制台将逐个处理它们。我按字符发送文本字符。有时控制台只执行ls和cd文件。我想也许这是因为即使控制台忙,我的程序也会不断发送字符,在这种情况下控制台不会收到传入的字符。
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.
我正在使用Delphi 7。
3 个解决方案
#1
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.
如果我正确地解释您的问题,您将文本发送到某种shell /命令行解释器,并且您希望它执行您的命令。
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).
通常命令行解释器输出某个提示符(如Linux系统上的$或DOS上的C:\)表示它们可以接受新命令。在发送另一个命令之前,需要读取输出以等待相应的提示。如果不这样,您发送的文本将被当前运行的命令(如您所经历的)输入。
#2
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.
洛萨是在正确的轨道上;你想要做的是,而不是使用ShellExecute,使用CreateProcess。查看Stack Overflow和Google的“控制台重定向” - 这将为您提供所需的信息。
#3
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.
您将命令发送到控制台。在命令运行时,程序将接收您发送的密钥。
#1
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.
如果我正确地解释您的问题,您将文本发送到某种shell /命令行解释器,并且您希望它执行您的命令。
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).
通常命令行解释器输出某个提示符(如Linux系统上的$或DOS上的C:\)表示它们可以接受新命令。在发送另一个命令之前,需要读取输出以等待相应的提示。如果不这样,您发送的文本将被当前运行的命令(如您所经历的)输入。
#2
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.
洛萨是在正确的轨道上;你想要做的是,而不是使用ShellExecute,使用CreateProcess。查看Stack Overflow和Google的“控制台重定向” - 这将为您提供所需的信息。
#3
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.
您将命令发送到控制台。在命令运行时,程序将接收您发送的密钥。