Recent

Author Topic: how to find application running in task manger  (Read 2292 times)

Packs

  • Sr. Member
  • ****
  • Posts: 416
how to find application running in task manger
« on: January 14, 2025, 12:03:48 pm »
Code: Pascal  [Select][+][-]
  1. program WatchdogApp;
  2.  
  3. uses
  4.   Classes, SysUtils, Process;
  5.  
  6. const
  7.   MAIN_APP = 'project1'; // Name of the main application executable (without extension)
  8.   CHECK_INTERVAL = 5000; // Check every 5 seconds
  9.  
  10. function IsProcessRunning(const AProcessName: string): Boolean;
  11. var
  12.   Process: TProcess;
  13.   Output: TStringList;
  14. begin
  15.   Result := False;
  16.   Process := TProcess.Create(nil);
  17.   Output := TStringList.Create;
  18.   try
  19.     Process.Executable := 'tasklist';   // linux pgrep
  20.     Process.Parameters.Add('-f');
  21.     Process.Parameters.Add(AProcessName);
  22.     Process.Options := [poUsePipes, poWaitOnExit,poNoConsole];
  23.     Process.Execute;
  24.  
  25.     Output.LoadFromStream(Process.Output);
  26.     WriteLn('total count',inttostr(Output.Count));
  27.     Result := Output.Count > 0; // If output is not empty, the process is running
  28.   finally
  29.     Output.Free;
  30.     Process.Free;
  31.   end;
  32. end;
  33.  
  34. procedure RestartProcess(const AProcessName: string);
  35. var
  36.   Process: TProcess;
  37. begin
  38.   Process := TProcess.Create(nil);
  39.   try
  40.     Process.Executable := AProcessName;
  41.     Process.Options := [poDetached]; // Run the process in the background
  42.     Process.Execute;
  43.   finally
  44.     Process.Free;
  45.   end;
  46. end;
  47.  
  48. begin
  49.   while True do
  50.   begin
  51.     if not IsProcessRunning(MAIN_APP) then
  52.     begin
  53.       WriteLn('Main application is not running. Restarting...');
  54.       RestartProcess(MAIN_APP);
  55.     end
  56.     else
  57.     begin
  58.       WriteLn('Main application is running.');
  59.     end;
  60.  
  61.     Sleep(CHECK_INTERVAL); // Wait before checking again
  62.   end;
  63. end.
  64.  
  65.  

I have written this code . it always start my project1.exe .

if project1.exe is running it should not start again

BSaidus

  • Hero Member
  • *****
  • Posts: 609
  • lazarus 1.8.4 Win8.1 / cross FreeBSD
Re: how to find application running in task manger
« Reply #1 on: January 14, 2025, 02:37:25 pm »
try with MUTEX'es
lazarus 1.8.4 Win8.1 / cross FreeBSD
dhukmucmur vernadh!

Packs

  • Sr. Member
  • ****
  • Posts: 416
Re: how to find application running in task manger
« Reply #2 on: January 14, 2025, 04:22:40 pm »
Please share any documents link

rvk

  • Hero Member
  • *****
  • Posts: 6655
Re: how to find application running in task manger
« Reply #3 on: January 14, 2025, 04:42:33 pm »
Have you tried tasklist -f in a cmd console to see what the output is???

Where did you get that code?

(But even running something like tasklist /fi "IMAGENAME eq project1.exe" it will give you several lines)

BTW. You don't even need to have a FPC program to do this... you can just use a batch file.

Something like:
Code: Text  [Select][+][-]
  1. tasklist /fi "ImageName eq MyApp.exe" /fo csv 2>NUL | find /I "myapp.exe">NUL
  2. if "%ERRORLEVEL%"=="0" echo Program is running

But then with a few extra lines to keep the batch running.
Notice it queries %ERRORLEVEL% to see if the find is successful.
So if you are doing this in FPC, you need to query the output to see if your process is in that output.

And yes... if this is for your own program... Mutex'es might be a better way to go.


bytebites

  • Hero Member
  • *****
  • Posts: 698

Packs

  • Sr. Member
  • ****
  • Posts: 416
Re: how to find application running in task manger
« Reply #5 on: January 15, 2025, 06:51:05 am »
Thank you .

This is really grate idea.

 

TinyPortal © 2005-2018