ostThreadMessage:用于向一个线程的消息队列发送一个消息。 idThread: DWORD; 目标线程的ID; Msg: UINT; 要发送的消息的数值;(一般是以WM_开头的常量) wParam: WPARAM; 第一个消息参数(其值和具体的消息有关); lParam: LPARAM 第二个消息参数(其值和具体的消息有关);例子:在SvcMgr.pas单元中: procedure TService.Controller(CtrlCode: DWord); begin PostThreadMessage(ServiceThread.ThreadID, CM_SERVICE_CONTROL_CODE, CtrlCode, 0); if ServiceThread.Suspended then ServiceThread.Resume; end;PeekMessage:用于从消息队列中获取一个消息,一般用于程序的主消息循环; lpMsg: TMsg; 指向消息结构的指针; hWnd: HWND; 窗口句柄,如果指定,则只接收指定窗口的消息; wMsgFilterMin,wMsgFilterMax: UINT 指定接收消息范围,如指定则只接收指定范围内的消息; wRemoveMsg: UINT 消息处理标志,指定消息处理后是否从队列中移去。例子:Forms.pas单元: function TApplication.ProcessMessage(var Msg: TMsg): Boolean; var Handled: Boolean; begin Result := False; if PeekMessage(Msg, 0, 0, 0, PM_REMOVE) then begin Result := True; if Msg.Message <> WM_QUIT then begin Handled := False; if Assigned(FOnMessage) then FOnMessage(Msg, Handled); if not IsHintMsg(Msg) and not Handled and not IsMDIMsg(Msg) and not IsKeyMsg(Msg) and not IsDlgMsg(Msg) then begin TranslateMessage(Msg); DispatchMessage(Msg); end; end else FTerminate := True; end; end;注:用PostThreadMessage发送的消息只能用GetMessage才可以接收到。用类似的代码, 就可处理你 1 的问题
2 的更简单, 用delphi 的向导生成一个线程, 然后 在 execute 函数中用while not terminated do begin ... sleep(1000);end;
1. use TEvent 2. use Buffer 3. thread2 get data, put it into buffer 4. thread1 pick up data from buffer Note: Use TEvent to synchronize thread1 and thread2, before/after they operate the buffer
http://www.ithack.net/Articles/delphi/2005050760357.htmlprocedure TThreadDataSet.Execute; var Msg : TMsg;
begin FreeOnTerminate := True; PeekMessage(Msg, 0, WM_USER, WM_USER, PM_NOREMOVE);
while not Terminated do begin if GetMessage(Msg, 0, 0, 0) then case Msg.Message of WM_OPENDATASET: WMOpenDataSet(Msg); WM_EXECUTESQL: WMExecSQL(Msg); end; end; end;
Integer(DataSet)DataSet是一個對象的引用 其實也就是一個地址值 轉為integer類型 傳給工作線程 可以直接使用。。 第二个问题呢?建立一个线程2,实现类似timer的功能,定时执行一个查看数据库的函数,有数据则调用发送函数。?? 一樣得呀 while true do // or other condition begin // your code Sleep(10000); end;
idThread: DWORD; 目标线程的ID;
Msg: UINT; 要发送的消息的数值;(一般是以WM_开头的常量)
wParam: WPARAM; 第一个消息参数(其值和具体的消息有关);
lParam: LPARAM 第二个消息参数(其值和具体的消息有关);例子:在SvcMgr.pas单元中:
procedure TService.Controller(CtrlCode: DWord);
begin
PostThreadMessage(ServiceThread.ThreadID, CM_SERVICE_CONTROL_CODE, CtrlCode, 0);
if ServiceThread.Suspended then ServiceThread.Resume;
end;PeekMessage:用于从消息队列中获取一个消息,一般用于程序的主消息循环;
lpMsg: TMsg; 指向消息结构的指针;
hWnd: HWND; 窗口句柄,如果指定,则只接收指定窗口的消息;
wMsgFilterMin,wMsgFilterMax: UINT 指定接收消息范围,如指定则只接收指定范围内的消息;
wRemoveMsg: UINT 消息处理标志,指定消息处理后是否从队列中移去。例子:Forms.pas单元:
function TApplication.ProcessMessage(var Msg: TMsg): Boolean;
var
Handled: Boolean;
begin
Result := False;
if PeekMessage(Msg, 0, 0, 0, PM_REMOVE) then
begin
Result := True;
if Msg.Message <> WM_QUIT then
begin
Handled := False;
if Assigned(FOnMessage) then FOnMessage(Msg, Handled);
if not IsHintMsg(Msg) and not Handled and not IsMDIMsg(Msg) and
not IsKeyMsg(Msg) and not IsDlgMsg(Msg) then
begin
TranslateMessage(Msg);
DispatchMessage(Msg);
end;
end
else
FTerminate := True;
end;
end;注:用PostThreadMessage发送的消息只能用GetMessage才可以接收到。用类似的代码, 就可处理你 1 的问题
在
execute 函数中用while not terminated do
begin
...
sleep(1000);end;
1、怎么把数据通过消息传递给线程?数据保存在msg里面吗?2、这个也太简单了吧,我怎么看别人说的用线程实现timer的功能怎么都那么复杂啊,不过好像这样也可以哦
对于第一个问题,aiirii老大的方法应该是主线程给该线程传消息,线程中接到消息后处理数据。但数据是通过消息传递进去的吗?哪位能给点更详细的解释或给点代码?
Classes.AllocateHwnd方法可以建立消息处理机制
然后,线程就等待消息的到来,处理
类似于程序主线程http://lysoft.7u7.net
ly_liuyang(Liu Yang)老大向来说话简单,可我是一点头绪都没有啊!
2. use Buffer
3. thread2 get data, put it into buffer
4. thread1 pick up data from buffer
Note: Use TEvent to synchronize thread1 and thread2, before/after they operate the buffer
另一种就是, 用全局内存变量来传递数据, 你接到消息后, 直接去读内存变量, 我给的代码, 可能还不是很清楚, 我再找一段给你
var
Msg : TMsg;
begin
FreeOnTerminate := True;
PeekMessage(Msg, 0, WM_USER, WM_USER, PM_NOREMOVE);
while not Terminated do begin
if GetMessage(Msg, 0, 0, 0) then
case Msg.Message of
WM_OPENDATASET: WMOpenDataSet(Msg);
WM_EXECUTESQL: WMExecSQL(Msg);
end;
end;
end;
begin
PostThreadMessage(ThreadID, WM_EXECUTESQL, Integer(DataSet), 0);
//Integer(DataSet)是什么?如果我想传一个记录进去呢?
end; wParam: WPARAM; 第一个消息参数(其值和具体的消息有关);
lParam: LPARAM 第二个消息参数(其值和具体的消息有关);
这两个参数随便定义成什么类型吗?
第二个问题呢?建立一个线程2,实现类似timer的功能,定时执行一个查看数据库的函数,有数据则调用发送函数。?? 一樣得呀 while true do // or other condition
begin
// your code
Sleep(10000);
end;
“另一种就是, 用全局内存变量来传递数据, 你接到消息后, 直接去读内存变量, ”
是一样的了?
这样线程安全吗?
var
Form1: TForm1;
EventHandle: THandle; {事件句柄}
ThreadHandle: THandle; {线程句柄}function ThreadFunction(Info: Pointer): Integer; stdcall;
var
Counter: Integer;
ObjRtn: Integer;
begin
while true do
begin
Form1.Button1.Caption := IntToStr(Counter);//可以在此加入查询数据库的代码
ObjRtn := WaitForSingleObject(EventHandle, 1000{时间间隔ms});
end;
ExitThread(1);
end;procedure TForm1.Button1Click(Sender: TObject);
var
ThreadID: Dword;
begin
EventHandle := CreateEvent(nil, True, False, 'MyEvent');
ThreadHandle := CreateThread(nil, 0, @ThreadFunction, nil, 0, ThreadId);
end;