主线程能不能给子线程sendmessage?(50分)

发消息未成功,当然子线程收不到
 
你把你的所有代码贴出来。
PostThreadMessage(SaveThreadArray[aThreadID].Savethread.Handle,WM_TASKMSG,integer(@TaskMSG),0);
这句话失败比不意味这句话错误,可能是其他地方错误。
The thread to which the message is posted must have created a message queue。
In the thread to which the message will be posted, call PeekMessage
(&msg, NULL, WM_USER, WM_USER, PM_NOREMOVE) to force the system to create the message queue.
 
这是消息的结构
TTaskMSG=record
TaskID:Integer ;
DBName:array[0..127] of char;
TableName:array[0..127] of char;
TempTableName:array[0..127] of char;
TaskFlag:array[0..1] of char;
BinStr:array[0..19] of char;
RandValue:Integer;
end;
PTaskMSG=^TTaskMSG ;
这是消息的付值和发送
TaskMSG.TaskID:=aTaskID;
TaskMSG.RandValue:=aRandValue;
strpcopy(TaskMSG.DBName,aDBName);
strpcopy(TaskMSG.TableName,aTableName);
strpcopy(TaskMSG.TempTableName,aTempTableName);
strpcopy(TaskMSG.TaskFlag,aTaskFlag);
strpcopy(TaskMSG.BinStr,aBinStr);
Result:=PostThreadMessage(SaveThreadArray[aThreadID].Savethread.ThreadID,WM_TASKMSG,integer(@TaskMSG),0);
线程中也收到了这个消息
但是数据取不出
case msg.Message of
WM_TASKMSG:
begin
p:=PTaskMSG(msg.wParam);
TaskID:=p^.TaskID;
RandValue:=p^.RandValue;
DBName:=p^.DBName;
TableName:=p^.TableName;
TempTableName:=p^.TempTableName;
TaskFlag:=p^.TaskFlag;
UpdatedColumnBin:=p^.BinStr;
里面都是空的
 
你怎么还是贴半截代码!
既然你不知道错在哪里,就应该贴出全部代码!
TaskMSG.TaskID:=aTaskID;
TaskMSG.RandValue:=aRandValue;
strpcopy(TaskMSG.DBName,aDBName);
strpcopy(TaskMSG.TableName,aTableName);
strpcopy(TaskMSG.TempTableName,aTempTableName);
strpcopy(TaskMSG.TaskFlag,aTaskFlag);
strpcopy(TaskMSG.BinStr,aBinStr);
你能保证正确吗?
如果不能,就全部贴出来!
aDBName
aTableName
等等是如何定义的?!

p:=PTaskMSG(msg.wParam);
TaskID:=p^.TaskID;
RandValue:=p^.RandValue;
DBName:=p^.DBName;
TableName:=p^.TableName;
TempTableName:=p^.TempTableName;
TaskFlag:=p^.TaskFlag;
UpdatedColumnBin:=p^.BinStr;
您能保证肯定正确吗?
如果不能
DBName
TableName
等等是如何定义的?
 
我也试了一下,但是用postthreadmessage时,返回的错误信息为:窗口不是子窗口,大侠
给看看:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
const
wm_usermsg=wm_user+20;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure ExitThread(Sender:Tobject);
private
{ Private declarations }
public
{ Public declarations }
end;

msgThread=class(tthread)
protected
procedure execute;override;
public
constructor Create(CreateSuspended: Boolean);
end;

var
Form1: TForm1;
bExit:boolean=false;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin

end;

{ msgThread }
constructor msgThread.Create(CreateSuspended: Boolean);
begin
inherited;
freeonterminate:= true;
messagebox(0,pchar('Thread has create'),'look',0);
end;

procedure msgThread.execute;
var
msg: TMsg;
dwHandleSignaled:DWORD;
begin

OnTerminate :=form1.ExitThread;
while true do
begin
Application.ProcessMessages ;
If peekmessage(msg, 0, 0, 0, PM_REMOVE) then
begin
case msg.message of
wm_usermsg:
begin
messagebox(0,pchar('Message has received!'),'look',0);
end;
end;

end;
if bExit then
break;
end;
messagebox(0,pchar('Exit'),'Info',0);
end;

procedure TForm1.Button1Click(Sender: TObject);
var
tmpThread:msgThread;
begin
tmpThread:=msgthread.Create(false);
if postthreadmessage(tmpThread.ThreadID,wm_usermsg,0,0) then
begin
showmessage('post message success!');
end;
showmessage(syserrormessage(getlasterror));
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
bExit:=true;
end;

procedure TForm1.ExitThread(Sender:Tobject);
begin
showmessage('This is thread exit!');
end;

end.
 
你的子线程CPU占用率好像是100%,这可不对了。
 
while true do
begin
// Application.ProcessMessages ;
If peekmessage(msg, 0, 0, 0, PM_REMOVE) then
begin
 
那线程代码好象没有错误呀?!
 
我不是不想全部贴出来,只是这个程序太长了
我现在已经能收到了,但有时候读消息的时候会报内存错误,
真奇怪
还有peekmessage()好像不会清掉已经处理过的消息,这个麻烦
 
哎,让你贴,你还有理由,我不是让你把你的所有程序都贴出来,是让你把相关的代码贴出来。
比如,你所有用到的变量是如何定义的?!
其实,我现在基本已经猜到你的错误了。
TaskMSG这个变量的定义有问题。
可能你要问,这个定义难道还有问题?
确实是这样,所以,有时候错误就在你认为最没有可能错误的地方。否则你怎么回找不到呢?!
我先告诉你解决办法。
把TaskMSG定义为全局变量或者为PTaskMSG,然后new,不要delete,在消息处理里面delete
>>peekmessage()好像不会清掉已经处理过的消息
这是不会的,可能是你上面的错误造成的
 
接受答案了.
 

Similar threads

S
回复
0
查看
959
SUNSTONE的Delphi笔记
S
S
回复
0
查看
779
SUNSTONE的Delphi笔记
S
S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
顶部