-5

i want to check if Anonymous thread is running,i have an idea which is to monitor the thread status every 1s, if it Exists, restart the work again..

I've got the thread ID, now how to check the status ?

procedure TForm2.Button5Click(Sender: TObject);
  begin
    TThread.CreateAnonymousThread(procedure   ()
    var i : integer;
    begin
      inc(i);

      label1.caption :=  TThread.Current.ThreadID.ToString;
    end).Start;
  end;
unknown
  • 65
  • 1
  • 2
  • 9
  • 11
    If you want to keep track of it, don't make it anonymous! – David Heffernan Dec 28 '16 at 19:58
  • well, make it what then ? i just know AnonymousThread & TTask, and i don't want to use TTask. @DavidHeffernan – unknown Dec 28 '16 at 20:06
  • Why not use a task. That sounds perfect. A thread that is not anonymous is just a plain thread. – David Heffernan Dec 28 '16 at 20:08
  • 1
    [System.Classes.TThread](http://docwiki.embarcadero.com/Libraries/en/System.Classes.TThread) – LU RD Dec 28 '16 at 20:08
  • its not suitable for long time http operation. it will become slower by time @DavidHeffernan – unknown Dec 28 '16 at 20:15
  • more explanation bro, please @LURD – unknown Dec 28 '16 at 20:16
  • 4
    Utter piffle. CreateAnonymousThread returns a TThread – David Heffernan Dec 28 '16 at 20:16
  • 1
    You can find examples/documentation by searching and learning. If you want to interact with a thread, set `TThread.FreeOnTerminate = false`. Use `TEvent` to signal and wait for things to happen. – LU RD Dec 28 '16 at 20:30
  • Some pointers here, [How to terminate anonymous threads in Delphi on application close?](http://stackoverflow.com/q/10401865/576719). – LU RD Dec 28 '16 at 20:45
  • @unknown "_its not suitable for long time http operation. it will become slower by time_" This statement demonstrates gaps in your understanding of threads. Listen to the advice of those who know. The difference between 'anonymous thread' and TThread is that TThread is _**not** anonymous_. Meaning you ***can hold*** a reference to the thread allowing you to interact with it. – Disillusioned Dec 28 '16 at 23:01
  • PS: I hope you're aware that the anonymous thread in your example does hardly any processing it will almost certainly have finished & been destroyed before you're anywhere near getting a chance to check its status. – Disillusioned Dec 28 '16 at 23:27

3 Answers3

3

Threads do not just stop. If your thread stops functioning it is because your code has a defect. Even the simple code in the question contains two defects. It reads a local variable before it is initialized and it uses a VCL method away from the main thread.

The entire premise of your question is wrong. You don't need to monitor whether or not your thread is still running. You simply need to fix the defects in your code so that it does not fail.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • OP's misguided idea aside, it may be useful to answer the actual question: how to determine the status of a thread based on ThreadID? – Disillusioned Dec 28 '16 at 23:29
  • The only thing I'm aware of that could be determined (non-intrusively) is whether a thread is STILL_ACTIVE using `GetExitCodeThread`. Ofc this takes a handle not a ThreadID. (Perhaps my comment should have said: "...determine if a thread is running...") – Disillusioned Dec 29 '16 at 03:24
  • 1
    @Craig Of course with an anonymous thread you'd have to obtain the handle and duplicate it before starting it. The thing is though is that the real problem is that asker has defective code and doesn't want to acknowledge that. Instead asker thinks that he can just restart the thread every now and then and hope for the best. – David Heffernan Dec 29 '16 at 03:27
  • I'm not entirely sure that OP really intends to "restart" the thread. I am certain OP lacks understanding of some fundamental thread concepts; and may think that while "checking if a thread is running" it is somehow blocked. (Obviously a misconceived notion to be sure.) But my suggestion to you was that it is feasible to take a snapshot of which threads were running at a point in time. It is something that may be useful for troubleshooting purposes. – Disillusioned Dec 29 '16 at 03:48
  • If you're not keen on the idea, then no worries. – Disillusioned Dec 29 '16 at 03:50
2

A better understanding of what threads are and how to use them, will help you. A thread is usually a way to get something done without holding up the user-interface. If you want the user to wait for something to finish, don't use a thread, just put the work code in the buttonclick event handler without any thread creating.

You said

every 1s, if it Exists, restart the work again

That makes no sense. If the thread exists, it's still busy working so there's no need to restart it. Maybe you should tell us what work you want the thread to do exactly.

In the example below (taken from Background Operations on Delphi Android, with Threads and Timers, you can see that the Synchronize procedure is called when the work is done, so that's how you know that that thread is done with it's work.

procedure TForm2.Button5Click(Sender: TObject);
begin
  TThread.CreateAnonymousThread(procedure ()
  var i : integer;
  begin
    inc(i); // some work here

    TThread.Synchronize (TThread.CurrentThread,
      procedure ()
      begin
        label1.Caption := TThread.Current.ThreadID.ToString;       
      end);

  end).Start;

end;
Freddie Bell
  • 2,186
  • 24
  • 43
-1

TThread.CreateAnonymousThread is a function which returns an instance of new Thread like this:

var ms:TThread;
ms:=TThread.CreateAnonymousThread( .....

You have an instance - ms "aka Thread" and you can work with this object...

quinz
  • 1,282
  • 4
  • 21
  • 33
vlad
  • 1