Recent

Author Topic: Get TProcess from ProcessID  (Read 2303 times)

yurkad

  • Full Member
  • ***
  • Posts: 173
  • development
Get TProcess from ProcessID
« on: April 23, 2019, 08:51:57 pm »
I have several values of id of Processes - several ProcID, obtained from list of running processes.
The objective is to suspend some processes.

TProcess has function Suspend. For use this function necessary obtain TProcess from ProcID.

var Process:TProcess;
Process.ProcessID must be equal that ProcID.

So how can be obtain TProcess from ProcID?



Cyrax

  • Hero Member
  • *****
  • Posts: 836
Re: Get TProcess from ProcessID
« Reply #1 on: April 23, 2019, 08:56:03 pm »
In this case, you can't use TProcess directly. However, you can check what system calls are made by TProcess.Suspend.

Thausand

  • Sr. Member
  • ****
  • Posts: 292
Re: Get TProcess from ProcessID
« Reply #2 on: April 23, 2019, 09:03:20 pm »

yurkad

  • Full Member
  • ***
  • Posts: 173
  • development
Re: Get TProcess from ProcessID
« Reply #3 on: April 23, 2019, 09:13:44 pm »
Thausand, no is Windows.

Thausand

  • Sr. Member
  • ****
  • Posts: 292
Re: Get TProcess from ProcessID
« Reply #4 on: April 23, 2019, 09:23:44 pm »
if windows then can use terminateprocess https://docs.microsoft.com/en-us/windows/desktop/api/processthreadsapi/nf-processthreadsapi-terminateprocess

then need handle. Is example c# but can make pascal https://stackoverflow.com/questions/1888863/how-to-get-main-window-handle-from-process-id

I not know if work but work in write  :)

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.
Re: Get TProcess from ProcessID
« Reply #5 on: April 23, 2019, 09:42:00 pm »
Function TProcess.Suspend : Longint;

begin
  If fpkill(Handle,SIGSTOP)<>0 then
    Result:=-1
  else
    Result:=1;
end;

On windows it calls suspendthread.


yurkad

  • Full Member
  • ***
  • Posts: 173
  • development
Re: Get TProcess from ProcessID
« Reply #6 on: April 23, 2019, 09:53:10 pm »
marcov, thanks, but what is HANDLE here? Is ProcID?

yurkad

  • Full Member
  • ***
  • Posts: 173
  • development
Re: Get TProcess from ProcessID
« Reply #7 on: April 23, 2019, 10:02:16 pm »
Just I found in JwaNative

function  NtSuspendProcess( hProcess : HANDLE ): NTSTATUS;

Here hProcess is ProcessID?


yurkad

  • Full Member
  • ***
  • Posts: 173
  • development
Re: Get TProcess from ProcessID
« Reply #9 on: April 23, 2019, 10:36:40 pm »
Thausand, Ok, I understood.
Then how can be use function NtSuspendProcess in my case?

yurkad

  • Full Member
  • ***
  • Posts: 173
  • development
Re: Get TProcess from ProcessID
« Reply #10 on: April 23, 2019, 11:36:04 pm »
My project must paint several images at once. The compliance time is critical in this case.
I made a time meter based on GetSystemTimeAsFileTime.
This meter shows that the painting time is less than one microsecond. It is good.
But at times (very frequent) meter shows 6 milliseconds. This is not acceptable in my case.
I understand that Os Windows is stopping my process to give rise to other processes.

For this painting period I increased the priority of my process to HIGH_PRIORITY_CLASS. The gain is only 1 millisecond or less.

Then I think about suspending other processes during this short time of 1 microsecond.

Another solution would be to lower the priority of other processes and then recover it.

The suspension or low priority only for 1 microsecond should not be a bad thing for other processes, I think.

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Get TProcess from ProcessID
« Reply #11 on: April 23, 2019, 11:46:01 pm »
If you really need it, set your priority to real-time. But don't forget to lower it again when you no longer need it!
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

ASerge

  • Hero Member
  • *****
  • Posts: 2222
Re: Get TProcess from ProcessID
« Reply #12 on: April 23, 2019, 11:53:21 pm »
These are both bad ideas. Consider using DirectX in a full-screen application if rendering is so critical. Normally, the rendering should be in low priority.

yurkad

  • Full Member
  • ***
  • Posts: 173
  • development
Re: Get TProcess from ProcessID
« Reply #13 on: April 24, 2019, 12:13:14 am »
lucamar, Thank, but practically without changes. :(

ASerge, why are you thinking that DirectX is going to force Os not to break for my process?
« Last Edit: April 24, 2019, 12:15:47 am by yurkad »

ASerge

  • Hero Member
  • *****
  • Posts: 2222
Re: Get TProcess from ProcessID
« Reply #14 on: April 24, 2019, 08:58:43 pm »
ASerge, why are you thinking that DirectX is going to force Os not to break for my process?
I don't think so. Windows is a multitasking operating system.

 

TinyPortal © 2005-2018