rbarzallo
asked on
how to handle postmessage in TThread Objects
I have a TThread object and desire to execute within a Postmessage, but so that reason does not execute????? Greetings
Ruben
Ruben
If I understood right, you want to start your thread when you receive a PostMessage?
Doing something like this when you receive the PostMessage should start it:
MyThread := TMyThread.Create(False);
If that is already what you did or I didn't understood correctly your question, don't hesitate to tell me!
GunDamn
Doing something like this when you receive the PostMessage should start it:
MyThread := TMyThread.Create(False);
If that is already what you did or I didn't understood correctly your question, don't hesitate to tell me!
GunDamn
ASKER
ok,
thread this in execution and within this execution desire to execute postmessage, thread executes the instruction but not the procedure assigned to the same one (to postmessage). The same it happens to me with postthreadmessage.
Ruben
thread this in execution and within this execution desire to execute postmessage, thread executes the instruction but not the procedure assigned to the same one (to postmessage). The same it happens to me with postthreadmessage.
Ruben
Are you posting your message with the right window handle?
I suppose you wish to post a message outSide of the Thread?
If not, maybe you could try to put your PostMessage procedure outside your thread.
If it still doesn't work, I will need a code sample to help you further.
GunDamn
I suppose you wish to post a message outSide of the Thread?
If not, maybe you could try to put your PostMessage procedure outside your thread.
If it still doesn't work, I will need a code sample to help you further.
GunDamn
it may do in the following way :
TRunThread =class(TThread)
protected
private
FWindowHandle: HWND;
procedure WndProc(var Msg: TMessage);
procedure DoAnything;
public
procedure Execute;override;
end;
procedure TRunThread.Execute;
begin
FWindowHandle := AllocateHWnd(WndProc);
DoSomething;
DeallocateHWnd(FWindowHand le);
end;
procedure TRunThread.DoAnything;
begin
...
end;
procedure TRunThread.WndProc(var Msg: TMessage);
begin
with Msg do
if Msg = WM_Thread then
try
DoAnything;
except
Application.HandleExceptio n(Self);
end
else
Result := DefWindowProc(FWindowHandl e, Msg, wParam, lParam);
end;
TRunThread =class(TThread)
protected
private
FWindowHandle: HWND;
procedure WndProc(var Msg: TMessage);
procedure DoAnything;
public
procedure Execute;override;
end;
procedure TRunThread.Execute;
begin
FWindowHandle := AllocateHWnd(WndProc);
DoSomething;
DeallocateHWnd(FWindowHand
end;
procedure TRunThread.DoAnything;
begin
...
end;
procedure TRunThread.WndProc(var Msg: TMessage);
begin
with Msg do
if Msg = WM_Thread then
try
DoAnything;
except
Application.HandleExceptio
end
else
Result := DefWindowProc(FWindowHandl
end;
in execute add next text:
while GetMessage(Msg,0, 0, 0) do
begin
TranslateMessage(Msg);
DispatchMessage(Msg);
end;
and remove procedure DoSomething;
while GetMessage(Msg,0, 0, 0) do
begin
TranslateMessage(Msg);
DispatchMessage(Msg);
end;
and remove procedure DoSomething;
in execute add next text:
while GetMessage(Msg,0, 0, 0) do
begin
TranslateMessage(Msg);
DispatchMessage(Msg);
end;
and remove procedure DoSomething;
while GetMessage(Msg,0, 0, 0) do
begin
TranslateMessage(Msg);
DispatchMessage(Msg);
end;
and remove procedure DoSomething;
ASKER
why not postmessage not processing wmcontrolclose???????
unit pruebas;
interface
uses
Messages, Forms, Classes, SysUtils,Windows, Dialogs;
const
wm_Controlclose = WM_User + 1;
type
ThSwt_Input = class(TThread)
public
constructor Create(CreateSuspended: Boolean);
procedure WmControlclose(var Msg: TMessage); Message wm_ControlClose;
private
{ Private declarations }
//
protected
procedure Execute; override;
procedure ProceseMessage;
end;
var
ThSwitch_In : ThSwt_Input;
implementation
{ Important: Methods and properties of objects in VCL can only be used in a
method called using Synchronize, for example,
Synchronize(UpdateCaption)
and UpdateCaption could look like,
procedure THDataQueueInput.UpdateCap
begin
Form1.Caption := 'Updated in a thread';
end; }
{ THDataQueueInput }
constructor ThSwt_Input.Create(CreateS
begin
Inherited Create(CreateSuspended);
end;
procedure ThSwt_Input.Execute;
begin
{ Place thread code here }
ProceseMessage;
end;
procedure ThSwt_Input.ProceseMessage
var
s1 : string;
begin
PostMessage(Handle,wm_Cont
s1 := 'continuo con la ejecucion del programa';
showmessage(s1);
// mas codigo del programa
end;
procedure ThSwt_Input.WmControlclose
var
s1 : string;
begin
// realiza proceso interno
s1 := 'ejecuta postmessage';
showmessage(s1);
end;
end.
ASKER
why postmessage not processing wmcontrolclose???????
unit pruebas;
interface
uses
Messages, Forms, Classes, SysUtils,Windows, Dialogs;
const
wm_Controlclose = WM_User + 1;
type
ThSwt_Input = class(TThread)
public
constructor Create(CreateSuspended: Boolean);
procedure WmControlclose(var Msg: TMessage); Message wm_ControlClose;
private
{ Private declarations }
//
protected
procedure Execute; override;
procedure ProceseMessage;
end;
var
ThSwitch_In : ThSwt_Input;
implementation
{ Important: Methods and properties of objects in VCL can only be used in a
method called using Synchronize, for example,
Synchronize(UpdateCaption)
and UpdateCaption could look like,
procedure THDataQueueInput.UpdateCap
begin
Form1.Caption := 'Updated in a thread';
end; }
{ THDataQueueInput }
constructor ThSwt_Input.Create(CreateS
begin
Inherited Create(CreateSuspended);
end;
procedure ThSwt_Input.Execute;
begin
{ Place thread code here }
ProceseMessage;
end;
procedure ThSwt_Input.ProceseMessage
var
s1 : string;
begin
PostMessage(Handle,wm_Cont
s1 := 'continuo con la ejecucion del programa';
showmessage(s1);
// mas codigo del programa
end;
procedure ThSwt_Input.WmControlclose
var
s1 : string;
begin
// realiza proceso interno
s1 := 'ejecuta postmessage';
showmessage(s1);
end;
end.
you must create Window in Thread and MessageQueue
like in my text
FWindowHandle := AllocateHWnd(WndProc);
like in my text
FWindowHandle := AllocateHWnd(WndProc);
procedure ThSwt_Input.Execute;
begin
FWindowHandle := AllocateHWnd(WndProc);
while GetMessage(Msg,0, 0, 0) do
begin
TranslateMessage(Msg);
DispatchMessage(Msg);
end;
DeallocateHWnd(FWindowHand le);
end;
you may postmessage out From Thread
var
Thread:ThSwt_Input;
procedure TForm1.Button1Click(Sender : TObject);
begin
Thread := ThSwt_Input.Create(True);
IsTread:=True;
Thread.OnTerminate := ThreadTerminate;
Thread.FreeOnTerminate := True;
Thread.Resume;
end;
procedure TForm1.Button2Click(Sender : TObject);
begin
SendMessage(Thread.FWindow Handle, WM_USER + 1, 0, 0);
end;
procedure ThSwt_Input.ProceseMessage ;
begin
Synchronize(DoAnything);
end;
procedure ThSwt_Input.DoAnything;
var
s1 : string;
begin
s1 := 'ejecuta postmessage';
showmessage(s1);
end;
begin
FWindowHandle := AllocateHWnd(WndProc);
while GetMessage(Msg,0, 0, 0) do
begin
TranslateMessage(Msg);
DispatchMessage(Msg);
end;
DeallocateHWnd(FWindowHand
end;
you may postmessage out From Thread
var
Thread:ThSwt_Input;
procedure TForm1.Button1Click(Sender
begin
Thread := ThSwt_Input.Create(True);
IsTread:=True;
Thread.OnTerminate := ThreadTerminate;
Thread.FreeOnTerminate := True;
Thread.Resume;
end;
procedure TForm1.Button2Click(Sender
begin
SendMessage(Thread.FWindow
end;
procedure ThSwt_Input.ProceseMessage
begin
Synchronize(DoAnything);
end;
procedure ThSwt_Input.DoAnything;
var
s1 : string;
begin
s1 := 'ejecuta postmessage';
showmessage(s1);
end;
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Not wishing to muddy the waters, but you might also look
at the api PostThreadMessage,
Iuse it for talking to threads, you dont need a window handle.
as
at the api PostThreadMessage,
Iuse it for talking to threads, you dont need a window handle.
as
oops sorry Bill, you said that.
Any way I do the following..
procedure TMyThread.Execute;
var
Msg : TMsg;
begin
PeekMessage(msg, 0, WM_USER, WM_USER, PM_NOREMOVE);// start the thread message queue
MyThreadId:=ThreadID;// stick the thread id somewhere I can find it.
MyStartup;
while True do begin
if Terminated then Break;//check if anyone called mythread.terminate-
IF NOT GetMessage(Msg, 0, 0, 0) THEN Break;//could peak here to prevent stall, this returns false if WM_Quit is posted
MyProcessMsg(msg);//I process the message
end;
MyCleanup;
end;
In the main program I call
PostThreadMessage(MyThread Id,wm_xxx, 0,0);
Any way I do the following..
procedure TMyThread.Execute;
var
Msg : TMsg;
begin
PeekMessage(msg, 0, WM_USER, WM_USER, PM_NOREMOVE);// start the thread message queue
MyThreadId:=ThreadID;// stick the thread id somewhere I can find it.
MyStartup;
while True do begin
if Terminated then Break;//check if anyone called mythread.terminate-
IF NOT GetMessage(Msg, 0, 0, 0) THEN Break;//could peak here to prevent stall, this returns false if WM_Quit is posted
MyProcessMsg(msg);//I process the message
end;
MyCleanup;
end;
In the main program I call
PostThreadMessage(MyThread
agree
its work :)
rbarzallo wants
postmessage inner Thread
its work :)
rbarzallo wants
postmessage inner Thread
Dear SMakarenko
I've rejected your proposed answer as Experts Exchange holds an experiment to work without the answer button.
See: https://www.experts-exchange.com/jsp/communityNews.jsp
Paragraph: Site Update for Wednesday, November 06, 2002
By this rejection the Asker will be notified by mail and hopefully he will take his responsibility to finalize the question or post an additional comment.
The Asker sees a button beside every post which says "Accept This Comment As Answer" (including rejected answers) -- so if he/she thinks yours is the best, you'll be awarded the points and the grade.
This Q hasn't been active lately, thus I've posted it in the CS Cleanup topic area.
I'll return to this Q in seven days to see if any coment has been added, if not then it's my intention that this question is:
-Answered by: SMakarenko
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER !
Thanks !
Jgould
Community Support Moderator
Experts Exchange
I've rejected your proposed answer as Experts Exchange holds an experiment to work without the answer button.
See: https://www.experts-exchange.com/jsp/communityNews.jsp
Paragraph: Site Update for Wednesday, November 06, 2002
By this rejection the Asker will be notified by mail and hopefully he will take his responsibility to finalize the question or post an additional comment.
The Asker sees a button beside every post which says "Accept This Comment As Answer" (including rejected answers) -- so if he/she thinks yours is the best, you'll be awarded the points and the grade.
This Q hasn't been active lately, thus I've posted it in the CS Cleanup topic area.
I'll return to this Q in seven days to see if any coment has been added, if not then it's my intention that this question is:
-Answered by: SMakarenko
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER !
Thanks !
Jgould
Community Support Moderator
Experts Exchange
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:
Accept SMakarenko's comment as answer
(will be submitted on 06/18/2003 to cleanup)
Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
Thank you,
Russell
EE Cleanup Volunteer
I will leave a recommendation in the Cleanup topic area that this question is:
Accept SMakarenko's comment as answer
(will be submitted on 06/18/2003 to cleanup)
Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
Thank you,
Russell
EE Cleanup Volunteer
Bill