TOraAlerter won't shoot event in a thread under certain circumstances
Posted: Sun 05 Mar 2006 10:25
This is not exactly a bug, I found a workaround, but it still feels weird to code that way.
Suppose I have a thread, and I want it to wait for event do something then wait for event again.
So it's something like this:
procedure TMyThread.OraAlerter1Event(Sender: TObject; Event, Message: String);
begin
SetEvent(alev);
somethinghappened:=True;
eventreceived:=True;
Continue:=False;
end;
procedure TMyThread.OraAlerter1TimeOut(Sender: TObject;var Continue: Boolean);
begin
SetEvent(alev);
eventreceived:=False;
somethinghappened:=True;
Continue:=False;
end;
TMyThread.Execute;
begin
While not(terminated) do
begin
OraAlerter.TimeOut:=1;{one is for debug purposes it maybe whatever}
OraAlerter.Interval:=0;
OraAlerter.Events:=ename;
eventrecieved:=False;
OraAlerter.Active:=True;
//not working code
WaitForSingleObject(alev,INFINITE);{event doesn't shoot this way}
//working code
While not(somethinghappened) do application.Processmessages; {event shoots this way}
OraAlerter.Active:=False;
If eventrecieved DoSomethingUseful;
end;
end;
Well in this case neither OraAlerter1Event nor OraAlerter1TimeOut won't shoot. But if you discard
events and make a cycle there with application.Processmessages then it is going to work. This is
not main thread, it's separate thread so it doesn't have to process no messages whatsoever.
Suppose I have a thread, and I want it to wait for event do something then wait for event again.
So it's something like this:
procedure TMyThread.OraAlerter1Event(Sender: TObject; Event, Message: String);
begin
SetEvent(alev);
somethinghappened:=True;
eventreceived:=True;
Continue:=False;
end;
procedure TMyThread.OraAlerter1TimeOut(Sender: TObject;var Continue: Boolean);
begin
SetEvent(alev);
eventreceived:=False;
somethinghappened:=True;
Continue:=False;
end;
TMyThread.Execute;
begin
While not(terminated) do
begin
OraAlerter.TimeOut:=1;{one is for debug purposes it maybe whatever}
OraAlerter.Interval:=0;
OraAlerter.Events:=ename;
eventrecieved:=False;
OraAlerter.Active:=True;
//not working code
WaitForSingleObject(alev,INFINITE);{event doesn't shoot this way}
//working code
While not(somethinghappened) do application.Processmessages; {event shoots this way}
OraAlerter.Active:=False;
If eventrecieved DoSomethingUseful;
end;
end;
Well in this case neither OraAlerter1Event nor OraAlerter1TimeOut won't shoot. But if you discard
events and make a cycle there with application.Processmessages then it is going to work. This is
not main thread, it's separate thread so it doesn't have to process no messages whatsoever.