Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: Question about Anonymous TThread XE7


This question is answered.


Permlink Replies: 2 - Last Post: Mar 25, 2015 4:15 AM Last Post By: Maycoll Trevezani
Maycoll Trevezani

Posts: 28
Registered: 1/20/11
Question about Anonymous TThread XE7  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Mar 24, 2015 2:08 PM
Hi guys,

I use XE7 and have two question about Anonymous TThread:

In this code, why Application.Terminate don't works?

 TThread.CreateAnonymousThread(
  procedure()
  begin
 
    TThread.Synchronize(TThread.CurrentThread, procedure()
    begin
      lblMsg.Text := 'Checking if Application open';
    end);
 
    vpHwnd := FindWindow('TApplication', 'MyApp');
 
    if vpHwnd > 0 then
    begin
      showmessage('Application already open');
      Application.Terminate; <<<<--------- Don't works
      //Self.close;                <<<<--------- Not also works (Self = MyForm)
    end;
end).start;

Other question is:

This is right way for stop the execution flow of Thread?

 TThread.CreateAnonymousThread(
  procedure()
  begin
 
    TThread.Synchronize(TThread.CurrentThread, procedure()
    begin
      lblMsg.Text := 'Checking if Application open';
    end);
 
    vpHwnd := FindWindow('TApplication', 'MyApp');
 
    if vpHwnd > 0 then
    begin
      showmessage('Application already open');
      
      TThread.CurrentThread.Terminate;
      TThread.CurrentThread.Destroy;
    end;
    
    // more code below ...
 
end).start;
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Question about Anonymous TThread XE7
Correct
Click to report abuse...   Click to reply to this thread Reply
  Posted: Mar 24, 2015 2:56 PM   in response to: Maycoll Trevezani in response to: Maycoll Trevezani
Maycoll wrote:

In this code, why Application.Terminate don't works?

You are calling it in the context of a worker thread rather than the main
UI thread. It posts a WM_QUIT message to the message queue of the calling
thread. So your main thread is not being signaled to shut down.

Also, ShowMessage() is not thread-safe (if you need to display a popup message
in a worker thread, use Winapi.MessageBox() instead), and Self.Close() must
be called in the main UI thread as well.

TThread.CreateAnonymousThread(
  procedure
  begin
    TThread.Synchronize(nil,
      procedure
      begin
        lblMsg.Text := 'Checking if Application open';
      end
    );
 
    vpHwnd := FindWindow('TApplication', 'MyApp');
    if vpHwnd <> 0 then
    begin
      TThread.Synchronize(nil,
        procedure
        begin
          ShowMessage('Application already open');
          Application.Terminate;
          //Self.Close;
        end
      );
    end;
  end
).Start;


This is right way for stop the execution flow of Thread?

You can call CurrentThread.Terminate(), but it is useless since your thread
code is never checking the CurrentThread.Terminated property. When your
procedure exits, the thread terminates, so just call Exit instead:

TThread.CreateAnonymousThread(
  procedure
  begin
    TThread.Synchronize(nil,
      procedure
      begin
        lblMsg.Text := 'Checking if Application open';
      end
    );
 
    vpHwnd := FindWindow('TApplication', 'MyApp');
    if vpHwnd <> 0 then
    begin
      TThread.Synchronize(nil,
        procedure
        begin
          ShowMessage('Application already open');
        end
      );
      Exit;
    end;
 
    // more code below ...
  end
).Start;


NEVER call CurrentThread.Destroy()!

--
Remy Lebeau (TeamB)
Maycoll Trevezani

Posts: 28
Registered: 1/20/11
Re: Question about Anonymous TThread XE7  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Mar 25, 2015 4:15 AM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Thanks a lot Remy Lebeau, you was very enlightening..

Working perfect.
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02