安全和良好的设计AllocateHWND能够响应多个线程吗?(Is safe and good design AllocateHWND to respond more than one thread?

编程入门 行业动态 更新时间:2024-10-28 06:30:30
安全和良好的设计AllocateHWND能够响应多个线程吗?(Is safe and good design AllocateHWND to respond more than one thread?)

众所周知,在需要在UI线程和工作线程之间进行通信的情况下,由于线程安全(句柄重建),必须创建隐藏窗口。

例如:

Form1具有N个动态创建的TProgressBar实例,其背景名称与后台运行相同。 总是保证WM_REFRESH只会在任务线程中调用。

Form1具有H : THandle property ,分配以下过程:

过程RefreshStat(var Message:TMessage); 消息WM_REFRESH;

在RefreshStat内部,在只有一个后台线程的情况下,我可以轻松地使用L和W参数来映射任务ID和位置。

我不知道标题是否说明了我想知道的内容,但让我们想象一下,如果我们有一个运行多个后台任务的应用程序。 在我的情况下,我使用TProgressBar报告已完成的进度。

AllocateHwnd是否保证所有消息在没有竞争条件的情况下到达隐藏窗口? 如果两个或多个任务同时发布消息会发生什么?

如果需要手动控制,我想知道除了在自定义消息中创建另一个消息循环系统之外还有别的事情要做。

我希望这个问题足够清楚。

It's known that, in cases when one needs comunicate between UI thread and working thread, an hidden window must be created because of thread safety(handle reconstruction).

For exemplify:

Form1 has N dynamicaly created TProgressBar instances with the same name of a background running . Is always garanteed that WM_REFRESH will only be called inside Task Thread.

Form1 has H : THandle property that allocates the following procedure:

procedure RefreshStat(var Message: TMessage); message WM_REFRESH;

Inside RefreshStat, in cases when there is only 1 background thread I could easily use L and W parameter to map Task Id and position.

I don't know if the title says what I want to know, but let's imagine if we have an application that has multiple background tasks running. In my case I use TProgressBar to report progress the done.

Does AllocateHwnd garantee that all messages arrives with no race condition the hidden window? What happens if two or more tasks post the message at the same time?

If this needs to be controled manually, I wonder if there is something else to do besides creating another message loop system in the custom message.

I hope the question is clear enough.

最满意答案

与线程关联的消息队列是线程安全队列。 来自多个其他线程的同步和异步消息都安全地传送,没有有害的日期比赛。 调用Windows消息API函数(如SendMessage和PostMessage)时无需任何外部同步。

如果两个线程同时向同一窗口发布或发送消息,则无法保证首先处理哪个消息。 这就是所谓的良性竞争条件。 如果您希望在另一个消息之前处理一条消息,则必须强制执行排序。

The message queue associated with a thread is a threadsafe queue. Both synchronous and asynchronous messages from multiple other thread are delivered safely no harmful date races. There is no need for any external synchronization when calling the Windows message API functions like SendMessage and PostMessage.

If two threads post or send messages to the same window at the same time, then there is no guarantee as to which message will be processed first. This is what is known as a benign race condition. If you want one message to be processed before the other then you must impose an ordering.

更多推荐

本文发布于:2023-08-03 08:42:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1385318.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   线程   safe   AllocateHWND   respond

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!