创建线程后,控制台中的WriteLn打印错误 [英] WriteLn in console after create thread print wrong

查看:95
本文介绍了创建线程后,控制台中的WriteLn打印错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用writeLn后,我在控制台中看到的文本出现问题.

I have a problem with text what I see in my console after use 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.              

使用此代码后,我没有按预期进入控制台.我得到:

After use this code i didnt get in my console what I expected. I get:

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并打印新线程已启动":

MyThread just use writeLn and print "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; 

你能帮我吗?我做错了什么?

Can you help me? What I do wrong?

看起来好像创建了新线程之后,我无法从TestApp在控制台上打印.运行

It looks like after create new thread I cant print on console from TestApp.Run

推荐答案

经过一些猜测,我制作了一个编译示例,但它在我的设置中可以正常使用.

With some guessing I made a compiling example, but it works as expected with my setup.

所以有两种可能性:

  • 您使用的特定版本的Lazarus有一个错误.
  • 您编译的代码与我猜测的不同.因此,请发布完整的编译示例以排除此情况.

这篇关于创建线程后,控制台中的WriteLn打印错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆