Multithreading 创建线程打印错误后在控制台中写入
使用writeLn后,我在控制台中看到的文本有问题 我的代码:Multithreading 创建线程打印错误后在控制台中写入,multithreading,console,pascal,lazarus,Multithreading,Console,Pascal,Lazarus,使用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'
(...)
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并打印“新线程已启动”:
你能帮我吗?我做错了什么
看起来在创建新线程后,我无法从TestApp在控制台上打印。通过一些猜测运行,我制作了一个编译示例,但它在我的设置中按预期工作 因此有两种可能性:
- 您正在使用的Lazarus的特定版本有一个bug
- 你编译的代码和我猜的不一样。因此,请发布一个完整的编译示例来排除这种可能性
- 您正在使用的Lazarus的特定版本有一个bug
- 你编译的代码和我猜的不一样。因此,请发布一个完整的编译示例来排除这种可能性
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;