为什么MessageBox不会在同步线程上阻止应用程序? [英] Why does a MessageBox not block the Application on a synchronized thread?

查看:193
本文介绍了为什么MessageBox不会在同步线程上阻止应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据了解并了解TThread类的方法,如果您同步代码,它实际上在主应用程序线程中执行(就像一个计时器/ buttonclick / etc)
我已经一直在玩,注意到一个MessageBox不会阻止主应用程序,但睡眠正如预期的那样。为什么是这样?

 键入
TTestThread = class(TThread)
private
procedure SynchThread ;
protected
procedure Execute;覆盖
public
构造函数Create(CreateSuspended:Boolean);
结束

程序TTestThread.SynchThread;
begin
MessageBoxA(0,'Hello','Test',0);
结束

程序TTestThread.Execute;
begin
同步(SynchThread)
end;

构造函数TTestThread.Create(CreateSuspended:Boolean);
开始
继承;
FreeOnTerminate:= True;
结束

程序StartThread;
var
TestThread:TTestThread;
begin
TestThread:= TTestThread.Create(FALSE);
结束


解决方案

这个答案有两个部分。 >

第1部分在如果MessageBox()/相关是同步的,为什么不'我的消息循环冻结? MessageBox函数不阻塞,它只创建一个带有自己的消息循环的对话框。



第2部分在 MessageBox文档


hWnd:要创建的消息框的所有者窗口的句柄。 如果此
参数为NULL,则消息框不具有所有者窗口。


当您显示一个模态对话框,Windows会禁用其所有者,但是如果您为第一个参数传递0,则不会禁用所有者,也不会禁用任何内容。因此,当显示消息框时,您的程序将继续处理消息(并对其进行响应)。



要更改此行为,请将表单的句柄作为第一个参数。例如:

 程序TTestThread.SynchThread; 
begin
MessageBoxA(Form1.Handle,'Hello','Test',0);
结束


As far as I understand and know the method of the TThread Class, if you synchronize your code, it actually get's executed in the main Application Thread (just like a timer/buttonclick/etc.) I've been playing around and noticed that a MessageBox DOES NOT block the main application, however sleep does just as expected. Why is that?

type
  TTestThread = class(TThread)
  private
    procedure SynchThread;
  protected
    procedure Execute; override;
  public
    constructor Create(CreateSuspended: Boolean);
  end;

procedure TTestThread.SynchThread;
begin
 MessageBoxA (0, 'Hello', 'Test', 0);
end;

procedure TTestThread.Execute;
begin
 Synchronize (SynchThread)
end;

constructor TTestThread.Create(CreateSuspended: Boolean);
begin
  inherited;
  FreeOnTerminate := True;
end;

procedure StartThread;
var
 TestThread : TTestThread;
begin
 TestThread := TTestThread.Create (FALSE);
end;

解决方案

There are two parts to this answer.

Part 1 is nicely explained in If MessageBox()/related are synchronous, why doesn't my message loop freeze?. The MessageBox function is not blocking, it merely creates a dialog box with its own message loop.

Part 2 is explained in the MessageBox documentation.

hWnd: A handle to the owner window of the message box to be created. If this parameter is NULL, the message box has no owner window.

When you display a modal dialog, Windows disables its owner, but if you pass 0 for the first parameter, there is no owner and nothing to disable. Therefore, your program will continue to process messages (and react to them) while the message box is displayed.

To change this behaviour, pass form's handle as a first parameter. For example:

procedure TTestThread.SynchThread;
begin
  MessageBoxA (Form1.Handle, 'Hello', 'Test', 0);
end;

这篇关于为什么MessageBox不会在同步线程上阻止应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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