worker thread

本文讲述了在使用工作线程(worker thread)时,为何不应直接操作GUI对象,以防止死锁。作者通过具体例子解释了死锁发生的原因,并提供了使用PostMessage发送用户定义消息到主线程来解决这个问题的方法。尽管如此,作者在尝试将工作线程改为UI线程时仍然遇到挂起问题,最终发现WaitForSingleObject导致了死锁。解决方案是让工作线程获取数据,然后通过PostMessage通知UI线程更新控件。
摘要由CSDN通过智能技术生成

最近用worker thread用的比较频繁,找到了一篇讲workthread讲的非常好的blog,请戳这里

ps,那篇blog写的非常好,但是你能想象吗?!这是作者2001写的,15年了

再结合我最近碰到的问题说两句,以下是blog中提到的

Worker threads and the GUI II: Don't touch the GUI

That's right. A worker thread must not touch a GUI object. This means that you should not query the state of a control, add something to a list box, set the state of a control, etc.

Why?

Because you can get into a serious deadlock situation. A classic example was posted on one of the discussion boards, and it described something that had happened to me last year. The situation is this: you start a thread, and then decide to wait for the thread to complete. Meanwhile, the thread does something apparently innocuous, such as add something to a list box, or, in the example that was posted, calls FindWindow. In both cases, the process came to a screeching halt, as all threads deadlocked.

Let's analyze these two situations in some detail, so you get a sense of what happened.

In my case, the list box sent a notification, via SendMessage, to its parent. This means the message went to its parent thread. But the parent thread was blocked, waiting for the thread to complete. But the thread couldn't complet

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值