How can I freeze a program?

Let's say I have a program that starts the processor and / or hard drive to such an extent that it is almost impossible to do anything else on this computer. Now I don’t want to kill this program because what it does is useful (this is a batch job that really is a heavy processor or disk, for example, it may be a ZIP of a few gigabytes of data files), but for a short time I need to do something else on this computer. Is there any way an external program could do to freeze this performance killer for a while?

This is similar to the old DOS option for switching between programs without multitasking.

Suppose the hypothetical program in question is a third-party product for which I have no source code, and there is no way to tell him to pause.

I know that I can change the priority class of a program, for example. in TaskManager, but this is not enough, I want to freeze it.

I am talking about Windows XP as an OS and would like to program the solution with Delphi. I have all the rights on the machine, so I could start something as an administrator, replace the files, and I could also install the service, if necessary.

+7
performance windows-xp delphi
Oct 31 '10 at 10:00
source share
4 answers

You can use my ProcessInfo component to pause all threads belonging to a process. This approach is similar to what Runner explained to you. The code would be something like this:

var Process : TProcessItem; AThread: TThreadItem; begin Process := ProcessInfo1.RunningProcesses.FindByName('notepad.exe'); if Assigned(Process) then begin for AThread in Process.Threads do AThread.SuspendThread; end; end; 

You can download the source code for the ProcessInfo form here.

+4
Oct 31 '10 at 13:34
source share

You can freeze it using Process Explorer : right-click on your program and select Suspend .

Here is a sample code for freezing programs from http://www.c-plusplus.de/forum/viewtopic-var-p-is-1460293.html , for brevity, edited and skipped error checking:

 #include <windows.h> _NtSuspendProcess NtSuspendProcess = (_NtSuspendProcess) GetProcAddress( GetModuleHandle( "ntdll" ), "NtSuspendProcess" ); HANDLE ProcessHandle = OpenProcess( PROCESS_ALL_ACCESS, FALSE, pid); NtSuspendProcess( ProcessHandle ); 
+14
Oct. 31 '10 at 10:02
source share

If you want to do this programmatically, you can use the description below here .

What it does is list all the threads in the process and then pause them. There is no SuspendProcess API, so this is a simulation of such a call.

Remember that this can have some good side effects. It depends on the process and how it is written.

I do not know any other way to do this in the Win32 / 64 API world. If you get to the kernel ground and use the NT * API, you have the NtSuspendProcess API. But this is undocumented, so it can change with any version of Windows or even with any service pack (not very likely).

The declaration "NtSuspendProcess" can be found in the JEDI ports of the Windows APIs.

+6
Oct. 31 '10 at 10:08
source share
 function OpenThread(dwDesiredAccess: DWORD; InheritHandle: Boolean; dwThreadID: DWORD): THandle; stdcall; external 'kernel32.dll'; function ResumeProcess(PID: DWORD):Boolean; var tid, snap: THandle; TE32: TThreadEntry32; begin Result := False; snap := CreateToolHelp32SnapShot(TH32CS_SNAPTHREAD, 0); TE32.dwSize := SizeOf(TThreadEntry32); Thread32First(snap, TE32); repeat if TE32.th32OwnerProcessID = PID then begin tid := OpenThread($0002, FALSE, TE32.th32ThreadID); ResumeThread(tid); Result := TRUE; CloseHandle(tid); end; until Thread32Next(snap, TE32) = false; CloseHandle(snap); end; function SuspendProcess(PID: DWORD): Boolean; var tid, snap: THandle; TE32: TThreadEntry32; begin Result := False; snap := CreateToolHelp32SnapShot(TH32CS_SNAPTHREAD, 0); TE32.dwSize := SizeOf(TThreadEntry32); Thread32First(snap, TE32); repeat if TE32.th32OwnerProcessID = PID then begin tid := OpenThread($0002, FALSE, TE32.th32ThreadID); SuspendThread(tid); Result := TRUE; CloseHandle(tid); end; until Thread32Next(snap, TE32) = false; CloseHandle(snap); end; 

Hope this helps

+2
Oct 31 '10 at 10:32
source share



All Articles