我在使用writeLn后在控制台中看到的文本有问题。
我的代码:
(...)
procedure TTestApp.Run;
var
MyThread:TMyThread;
begin
writeLn('Start new program');
writeLn('Start new thread');
MyThread:=TMyThread.Create;
sleep(1000);
writeLn('Next part of program');
end;
var
TestApp: TTestApp;
begin
TestApp := TTestApp.Create;
TestApp.Run;
TestApp.Free;
end.
使用此代码后,我没有在我的控制台中得到我的预期。我明白了:
Start new program.
Start new thread
New thread started
Start new thread
但我期待:
Start new program.
Start new thread
New thread started
Next part of program.
MyThread只使用writeLn并打印“New thread started”:
TMyThread = class(TThread)
protected
procedure Execute; override;
public
constructor Create;
end;
constructor TMyThread.Create;
begin
inherited Create(false);
end;
procedure TMyThread.Execute;
var
quit:boolean;
begin
FreeOnTerminate := False;
writeLn('New thread started');
quit:=false;
repeat
//some infinite stuff
until quit;
end;
你能帮帮我吗?我做错了什么?
看起来在创建新线程后我无法在TestApp.Run
的控制台上打印答案 0 :(得分:2)
通过一些猜测,我做了一个编译示例,但它在我的设置中按预期工作。
所以有两种可能性: