【提问】关于建立线程的消息队列,该怎么处理
【提问】关于建立线程的消息队列
近期学习多线程,跨线程传消息前要令os为线程建立消息队列,方法是在线程调用user或gdi的api(大概意思是这样,表述是否准确未知),比如PostThreadMessage、GetMessage、PeekMessage
MSDN有文提出如下方法:
The thread to which the message is posted must have created a message queue, or else the call to PostThreadMessage fails. Use one of the following methods to handle this situation:
Call PostThreadMessage. If it fails, call the Sleep function and call PostThreadMessage again. Repeat until PostThreadMessage succeeds.
大概就是循环PostThreadMessage,直到成功
我的程序有主线程MainThread和子线程SubThread
我的疑问如下:
1、MainThread内向SubThread循环PostThreadMessage(如下),但SubThread没有任何GetMessage/PeekMessage等接收消息的语句,OS依然是会为SubThread建立消息队列吗?
2、反过来,任何线程都不PostThreadMessage,而只有SubThread自己有接收消息代码(如下),OS是否仍然会为SubThread建立消息序列?
3、由于SubThread要向MainThread发消息,因此我加了MainThread的id到SubThread.MainThreadHandle,其类型是THandle。那么当SubThread结束的时候,怎样释放这个SubThread.MainThreadHandle?THandle不能nil,而free又应当是MainThread自己去做。
4、当SubThread挂起,MainThread继续PostThreadMessage(SubThread.ThreadID, 0, 0, 0);
OS依然会将其加入到SubThread的消息队列吗?我的测试是会,特求证
5、是否delphi的所有基础类型(Integer/String/Cardinal/Boolean等等)的局部变量和实例变量(非指针),都不需要 := nill和Free/FreeAndNil的?
请指点,谢谢
------解决方案--------------------
有个TThreadList可以使用一下,比较方便
------解决方案--------------------
1. The thread to which the message is posted must have created a message queue, or else the call to PostThreadMessage fails.
所以不会建立消息队列。
2. MSDN中对PostThreadMessage的参数idThread的注解中有:
The system creates a thread's message queue when the thread makes its first call to one of the User or GDI functions. 而PeekMessage就是user32.dll的一个导出函数,所以会建立消息队列。
3. SubThread结束的时候,不要释放这个SubThread.MainThreadHandle,因为它就相当于栈上的一个局部变量。
4. 在PostThreadMessage的MSDN 帮助中没找到对于线程是否挂起的限制,所以会加入到队列。
5. 是。
近期学习多线程,跨线程传消息前要令os为线程建立消息队列,方法是在线程调用user或gdi的api(大概意思是这样,表述是否准确未知),比如PostThreadMessage、GetMessage、PeekMessage
MSDN有文提出如下方法:
The thread to which the message is posted must have created a message queue, or else the call to PostThreadMessage fails. Use one of the following methods to handle this situation:
Call PostThreadMessage. If it fails, call the Sleep function and call PostThreadMessage again. Repeat until PostThreadMessage succeeds.
大概就是循环PostThreadMessage,直到成功
我的程序有主线程MainThread和子线程SubThread
我的疑问如下:
1、MainThread内向SubThread循环PostThreadMessage(如下),但SubThread没有任何GetMessage/PeekMessage等接收消息的语句,OS依然是会为SubThread建立消息队列吗?
- Delphi(Pascal) code
while PostThreadMessage(SubThread.ThreadID, 0, 0, 0) do begin sleep(100); end
2、反过来,任何线程都不PostThreadMessage,而只有SubThread自己有接收消息代码(如下),OS是否仍然会为SubThread建立消息序列?
- Delphi(Pascal) code
procedure SubThread.Excute; var msg: TagMsg; begin while (not teminated) and PeekMessage(msg, 0, 0, PM_REMOVE) do begin Sleep(100); end; end;
3、由于SubThread要向MainThread发消息,因此我加了MainThread的id到SubThread.MainThreadHandle,其类型是THandle。那么当SubThread结束的时候,怎样释放这个SubThread.MainThreadHandle?THandle不能nil,而free又应当是MainThread自己去做。
4、当SubThread挂起,MainThread继续PostThreadMessage(SubThread.ThreadID, 0, 0, 0);
OS依然会将其加入到SubThread的消息队列吗?我的测试是会,特求证
5、是否delphi的所有基础类型(Integer/String/Cardinal/Boolean等等)的局部变量和实例变量(非指针),都不需要 := nill和Free/FreeAndNil的?
请指点,谢谢
------解决方案--------------------
有个TThreadList可以使用一下,比较方便
------解决方案--------------------
1. The thread to which the message is posted must have created a message queue, or else the call to PostThreadMessage fails.
所以不会建立消息队列。
2. MSDN中对PostThreadMessage的参数idThread的注解中有:
The system creates a thread's message queue when the thread makes its first call to one of the User or GDI functions. 而PeekMessage就是user32.dll的一个导出函数,所以会建立消息队列。
3. SubThread结束的时候,不要释放这个SubThread.MainThreadHandle,因为它就相当于栈上的一个局部变量。
4. 在PostThreadMessage的MSDN 帮助中没找到对于线程是否挂起的限制,所以会加入到队列。
5. 是。