Threads

How can I tell if my method is executed from the VCL thread or from another thread (a TThread descendant)?

procedure TFoo.Bar;
begin
  if InVclThread then
    This
  else
    That;
end;
LVL 2
__alexAsked:
Who is Participating?
 
Lee_NoverConnect With a Mentor Commented:
function InVCLThread: Boolean;
begin
     Result:=GetWindowThreadProcessId(Application.Handle) = GetCurrentThreadId;
end;

should do :)
0
 
shaneholmesCommented:
In your threads execute method, place a flag variable

var
 InVclThread : boolean

which you can set to true in the thread


then you can test it outside the thread


Shane
0
 
shaneholmesCommented:
procedure TMyClientThread.Execute;
begin
 //call your method here
 InVclThread:= True;
end;

Shane
0
 
__alexAuthor Commented:
Thanks Lee!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.