线程间的通讯(200分)

  • 主题发起人 Tigerchamp
  • 开始时间
T

Tigerchamp

Unregistered / Unconfirmed
GUEST, unregistred user!
主线程和其中一个线程之间的通讯采用Event事件,子线程不断循环
检测Event状态,而此Event状态是在一个Socket的OnRead事件中设置的,
但我的子线程一启动,就循环个没完没了,好象将OnRead事件的消息都屏
蔽了,因此程序无法正常运行。
不知如何解决,是不是要自己用API接收网络数据,然后设置Event;
不能被动地通过OnRead消息来接收?
望各位大侠指点迷津!
 
有没有PostMessage;
 
有两个解决办法, 可以同时使用:
一.在你的子线程的循环中加以下一句:
Application.ProcessMessages
二.在子线程开始前, 把子线程的优先级设得低一点,如
curthread := MailThread.create(true);
curthread.priority := tpLower;
 
ProcessMessages
 
Application.ProcessMessage 我试过,但在子线程的Unit的编译过程中不认识
Application,说它是Undeclared Identifier.是不是要在Uses语句中加入dpr
文件名?
优先级别好象没有用。
PostMessage该如何使用,或者谁作过这方面的编程,希望能说得更详细一点,或
贴一点Sample什么的。
 
Application.ProcessMessage 我试过,但在子线程的Unit的编译过程中不认识
Application,说它是Undeclared Identifier.是不是要在Uses语句中加入dpr
文件名?
优先级别好象没有用。
PostMessage该如何使用,或者谁作过这方面的编程,希望能说得更详细一点,或
贴一点Sample什么的。
 
在Uses语句中加入 Forms
没什么用法
 
The PostMessage function places (posts) a message in the message queue associated with the thread that created the specified window and then
returns without waiting for the thread to process the message. Messages in a message queue are retrieved by calls to the GetMessage or PeekMessage function.
BOOL PostMessage(
HWND hWnd, // handle of destination window
UINT Msg, // message to post
WPARAM wParam, // first message parameter
LPARAM lParam // second message parameter
);
 
在Application.OnIdle中监测Event的状态更好, 第一, 不浪费系统资源,
第二, 系统激活这个事件, 编程简单.
 
API 函数 WaitForSingleObjectEx 可以解决。(不过,我不太熟悉Delphi,
VC 中可以办到)
 
postmessage 只要添加forms,
没有参数.
你的问题是没有提交控制权给系统.
 
1.引用 forms,使用Application.ProcessMessage
2.合理规划你的程序,应该不用线程也可以,因为各种Socket控件都有自己独立的
线程。
 
PostThreadMessage
 
我的问题已解决,但原因好象和我提出的问题并不太一样,
是由于Synchronize与Waitfor的冲突。
多谢大家的热心帮助!
 
多人接受答案了。
 
顶部