Delphi Multi-Threading Message Loop

后端 未结 3 709
执笔经年
执笔经年 2020-12-28 10:39

My application has several threads: 1) Main Thread 2) 2 Sub-Main Threads (each with Message Loop, as shown below), used by TFQM 3) n Worker Threads (simple loop, containing

3条回答
  •  爱一瞬间的悲伤
    2020-12-28 10:54

    If I may point to few problems in your code ...

    1) You're not checking output of AllocateHwnd. Yes, most probably it will never fail, but still ...

    2) AllocateHwnd belogs OUT of try..finally! If it fails, DeallocateHwnd should not be called.

    3) AllocateHwnd is not threadsafe. If you call it from multiple threads at the same time, you can run into poblems. Read more.

    As Davy said, use MsgWaitForMultipleObjects instead of creating hidden message window. Then use PostThreadMessage to send messages to thread.

    If I may put a plug for a totally free product here - use my OmniThreadLibrary instead. Much simpler than messing directly with Windows messaging.

提交回复
热议问题