Solved

How do I pause a process other than my own and then later allow it to continue?

Posted on 2003-10-30
55
909 Views
Last Modified: 2010-04-05
Hi

If I wanted to "Freeze a process" in Win 95,98,2000 or Xp, how would I do it. This process is not my own it could be notepad or any other application.

Then at my own discretion would allow it to continue running where it left off.

I do not want the process ended or destroyed. This process must be paused only to be continued later.

Please if there is any one that knows the answer to this let me know.

Thank you,
Horatio Huxham



 
0
Comment
Question by:HoratioH
  • 28
  • 18
  • 4
  • +2
55 Comments
 
LVL 8

Expert Comment

by:gmayo
ID: 9652643
You could run it in a "debug" mode. DebugActiveProcess(procid), DebugBreak, and ContinueDebugEvent may be of use. Look them up in the Win32 SDK.

Geoff M.
0
 

Author Comment

by:HoratioH
ID: 9655878
Thanks. I think this is going to help.

Could you for a final run please give me some sample code.

Could you please give the code to get process ID and thread ID and then to DebugBreak the process.

and then ContinueDebugEvent the process.

With the params as they are expected.

Thank you,
Horatio
0
 
LVL 8

Expert Comment

by:gmayo
ID: 9656121
I've never done it, sorry!

Geoff M.
0
 
LVL 6

Expert Comment

by:GloomyFriar
ID: 9656611
>Could you please give the code to get process ID and thread ID and then to DebugBreak the process.
Which information do you have about the process which you want to "freeze" (the name of exe file, caption of the main window, ... or what)?
0
 

Author Comment

by:HoratioH
ID: 9656640
exe name
0
 
LVL 6

Expert Comment

by:GloomyFriar
ID: 9656673
And if the program is started 2 times? It is necessary to stop both copies?
0
 
LVL 6

Expert Comment

by:GloomyFriar
ID: 9656770
Here is the way you can get process ID:
1.EnumProcesses
2.OpenProcess
3. GetModuleBaseName or
0
 
LVL 6

Expert Comment

by:GloomyFriar
ID: 9656779
Ooops. Sorry, I've pressed to submit occasionly.
Wait for a while, I'm looking for decision.
0
 
LVL 6

Expert Comment

by:GloomyFriar
ID: 9656798
By the way why do you want to "freeze" the process?
Is it possible, that the process have several threads?
0
 

Author Comment

by:HoratioH
ID: 9656940
Yes, the process could have more than one thread.

If I could "freeze" all the threads off all the copies of the exe that will be great.

0
 
LVL 6

Expert Comment

by:GloomyFriar
ID: 9656988
Here is the way you can get process ID:
1.EnumProcesses - you'll get all process IDs
2.OpenProcess - convert process ID to process HANDLE - you need at next step
3.EnumProcessModules - you'll get handles of all modules.
4.GetModuleBaseName or GetModuleFileNameEx - you'll get filename of the module
5.Compare the module name with the target process exe name. If they are equal, then BINGO!.

If you really need working source code just tell me. But it takes some time.
0
 

Author Comment

by:HoratioH
ID: 9657096
Please ..  ... . gimmeee some working source code.

I like to get the experts way of doing things :)

Please could you give me some working source code on:

- Get all the processes.
- Look for the exe that you want to "Freeze".
- Then Freeze it

- If you click another btn then the process should continue as if nothing has happened.

Questions:
How manny processes can you freeze at one time?

- Source code please -
How do I monitor if they are realy frozen?
How do I make sure that they are running again after i "Play" them again?

If you can give me this I will give you all my points that I have :)

Thank you

0
 
LVL 6

Expert Comment

by:GloomyFriar
ID: 9658542
>If you can give me this I will give you all my points that I have :)
It's sounds good ;)

I'll try to make such a programm. But it'll take some time.

Nevertheless I wanna know the reasons why you want to freeze the applications?
0
 

Author Comment

by:HoratioH
ID: 9659026
Well when I do a defrag on my pc I want to click on a btn and make all the applications, that is currently slowing down my defrag, "Freeze" until my defrag is completed.

Then .... "UN- Freeze all"

Good idea ..
0
 
LVL 6

Expert Comment

by:GloomyFriar
ID: 9659621
If so then there is more easy solution from my point of view.
Here it is. Firstly get all processes handles from EnumProcesses and then just give them the lowest priority class (IDLE_PRIORITY_CLASS) by calling SetPriorityClass.

How about such idea? ;-)
0
 
LVL 6

Expert Comment

by:GloomyFriar
ID: 9659642
I'm waiting for you answer to make it clear whether to work with it on days off or not.
0
 
LVL 5

Expert Comment

by:DeerBear
ID: 9660202
Hi,

If I were you, I would try to figure out better what you want to do.
I think that you may be well off suspending all the threads of the
process, that should make it.

The way to do this has already been shown to you.

As to the source code, I'd give you some, but it's not my PC and I haven't got Delphi in front of me.

Please bear in mind that doing system programming without
knowing well the system behind you can be quite dangerous.

Please, /do/ read some books on the topic before delving into
such a subject.

HTH,

Andrew
0
 

Author Comment

by:HoratioH
ID: 9662270
Hi

Does setting the priority to IDLE freeze the process?

Thanks for all the help. I would like to go with the freeze thing. I would like to freeze them and the unfreeze them.

What are the dangers in doing this?

If there are any dangers please let me know.

All I need now is source code in how to get all the processIDs and all the ThreadIDs.

The rest I will be able to do.






0
 
LVL 5

Expert Comment

by:Hypoviax
ID: 9667504
You have said that you need to enumerate the running processes.

The below code will terminate an app from the string name. This may be useful. However you can modify if to add the PID etc to a listbox or listview etc:


function KillTask(ExeFileName: string): integer;
const
  PROCESS_TERMINATE=$0001;
var
  ContinueLoop: BOOL;
  FSnapshotHandle: THandle;
  FProcessEntry32: TProcessEntry32;
begin
  result := 0;
  FSnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
  FProcessEntry32.dwSize := Sizeof(FProcessEntry32);
  ContinueLoop := Process32First(FSnapshotHandle, FProcessEntry32);
  while integer(ContinueLoop) <> 0 do begin
// !!USE FProcessEntry32.szexefile to enumerate!! //
    if (StrIComp(PChar(ExtractFileName(FProcessEntry32.szExeFile)), PChar(ExeFileName)) = 0)
       or (StrIComp(FProcessEntry32.szExeFile, PChar(ExeFileName)) = 0)  then
      Result := Integer(TerminateProcess(OpenProcess(
                        PROCESS_TERMINATE, BOOL(0), // BOOL(0) means 'false'
                        FProcessEntry32.th32ProcessID), 0));
    ContinueLoop := Process32Next(FSnapshotHandle, FProcessEntry32);
  end;
  CloseHandle(FSnapshotHandle);
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  KillTask(Edit1.Text); // 'notepad.exe' for example
end;

Hope this helps,

Hypoviax
0
 
LVL 5

Expert Comment

by:Hypoviax
ID: 9667511
You may also find this useful. It will return the filepath of an executable given the PID:

var
  c : cardinal;
  me : TModuleEntry32;
begin
  c := CreateToolhelp32Snapshot(TH32CS_SnapModule, PROCESS ID IN HERE);
  me.dwSize := sizeof(me);
  Module32First(c, me);

  ShowMessage(me.szExePath); //The FILE PATH
  CloseHandle(c);
end;

Regards,

Hypoviax
0
 

Author Comment

by:HoratioH
ID: 9669126
Thanks

I now have the processes. Now I need to be able to un-freeze a process after I have used DebugActiveProcess(procid) and  DebugBreak.

I have called ContinueDebugEvent  but it does not work :(

If the suspending thread is a better Idea then I need to know how to use it so that for a process I will be able to suspend all threads for that process and then un-suspending the process and al its threads.

 
0
 
LVL 6

Expert Comment

by:GloomyFriar
ID: 9670042
It's me again.
The problem is that you can not stop debugging after it's started.
You can do it only under Win XP:

DebugActiveProcessStop:
Requirements
  Windows NT/2000/XP: Included in Windows XP and Windows .NET Server.
  Windows 95/98/Me: Unsupported
0
 
LVL 6

Expert Comment

by:GloomyFriar
ID: 9670257
Here is the code.
It handle only one copy of the target application. But it only a little bit more complicated to handle several copies.

/////////////////////////////////////////////////////////////////////////////////////////////////
unit main;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Buttons, StdCtrls, TlHelp32;

type
  TForm1 = class(TForm)
    edtFileName: TEdit;
    SpeedButton1: TSpeedButton;
    procedure SpeedButton1Click(Sender: TObject);
  private
    { Private declarations }
    ps_id: DWORD;
    ps_handle: THANDLE;
    event: DEBUG_EVENT;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

uses th_debug;

{$R *.DFM}
procedure TForm1.SpeedButton1Click(Sender: TObject);
var hSnapshoot: THandle;
    pe32: TProcessEntry32;
    DebugActiveProcessStop: function(dwProcessId: DWORD): boolean; cdecl;
    hLib: THandle;
    th: TDebugThread;
begin
  if SpeedButton1.Down then begin
    { Need to suspend process }
    SpeedButton1.Caption := 'Release';

    hSnapshoot := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
    if (hSnapshoot = -1) then begin
      { Some error }
      Exit;
    end;

    pe32.dwSize := SizeOf(TProcessEntry32);
    if (Process32First(hSnapshoot, pe32)) then
    repeat
      if (CompareText(PChar(ExtractFileName(pe32.szExeFile)),
                      PChar(edtFileName.Text)) = 0) or
         (CompareText(pe32.szExeFile,
                      PChar(PChar(edtFileName.Text))) = 0)  then begin
        { Found it !}
        ShowMessage('Got it!');
        ps_id := pe32.th32ProcessID;
        pid := ps_id;
        th := TDebugThread.Create(False);
        break;
      end;
    until not Process32Next(hSnapshoot, pe32);

    CloseHandle (hSnapshoot);
  end else begin
    { Need to release process }
    SpeedButton1.Caption := 'Suspend';

    evt.SetEvent;
  end;
end;

end.

///////////////////////////////////////////////////////////////////////////////////////

unit th_debug;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Buttons, StdCtrls, TlHelp32, syncobjs;

type
  TDebugThread = class(TThread)
  private
    { Private declarations }
    event: DEBUG_EVENT;
  protected
    procedure Execute; override;
  end;

var  
  evt: TEvent;
  pid: DWORD;

implementation
{ TDebugThread }

procedure TDebugThread.Execute;
begin
 if DebugActiveProcess(pid) then begin
  evt := TEvent.Create(0, True, False, '');

  while not Terminated do begin
    if WaitForDebugEvent(event, 1000) then begin
      while evt.WaitFor(10) <> wrSignaled      do begin Sleep(100); end;
      if event.dwDebugEventCode = EXIT_PROCESS_DEBUG_EVENT then break;
      ContinueDebugEvent(event.dwProcessId, event.dwThreadId, DBG_CONTINUE);
    end;
  end;
 end;
end;

end.
0
 

Author Comment

by:HoratioH
ID: 9670750
This code is great :)

I am going to test it tonight :)

Will this work on 98/nt/2000/xp ?

0
 

Author Comment

by:HoratioH
ID: 9670854

Do you think suspend thread is a better way to go?

If so could you give me some code that deals with the suspend thread.
I would like to look at all my options. If there is another way to freeze and un-freeze processes safely then please tel me. I will increase the points for the safest and best way to go about freezing and unfreezing processes.

If the suspend way is the best then:

I would like code that could suspend all threads of a process and also then be able to cancel the suspend on all the threads of the process.

Will the whole application that I suspend be "Frozen"?

This is the final stuff I need on this question.

Give me this and I will hand you the points.

Thanks a lot :)
0
 
LVL 6

Expert Comment

by:GloomyFriar
ID: 9671667
>Will this work on 98/nt/2000/xp ?
I've tested it under Win2k.
0
 
LVL 6

Expert Comment

by:GloomyFriar
ID: 9672047
>If the suspend way is the best
I don't know the way to suspend a thread in remote process.

I ask you once again. What are the reasons to freeze the applications.
Could you describe it more detailed.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 6

Expert Comment

by:GloomyFriar
ID: 9673287
>I don't know the way to suspend a thread in remote process.
I'm trying to do that using DuplicateHandle. The results will be tomorrow.
0
 

Author Comment

by:HoratioH
ID: 9673459
The code you gave me will allow the process to continue but when I kill my app all the processes that has been "Frozen" terminate :(

This is bad.

I want to be able to freeze the process and when i have allowed it to run again I want to be able to close my app and go on with life.

0
 
LVL 5

Expert Comment

by:Hypoviax
ID: 9675567

To HoratioH,
I don't know this guy's close but how about you put in an onclose event in your program that will unfreeze it. Also look through the code and see what is the condition that has the ability to terminate the processes. Modify this so that it will not terminate them once your program has closed.

To Gloomyfriar, there are a billion reasons why HoratioH may want to freeze a process. For a start it makes an excellent security feature (just what i'm looking for) and like he says:

'Well when I do a defrag on my pc I want to click on a btn and make all the applications, that is currently slowing down my defrag, "Freeze" until my defrag is completed.'


Regards,

Hypoviax
0
 

Author Comment

by:HoratioH
ID: 9676721
The problem here is that my app creates a new instance of the process and set it to debug mode. The debuged process now have my application as owner and will teerminate when I terminate my app :(

This is a big problem for me.

You see.... I want my app to auto detect the start of defrag and then send all the processes that I specify in to "Freeze".

Then when the defrag is complete I want this program to Un-Freeze the processes and terminate it self.

That is what I want to do.

Now I need to be able to freeze and unfreeze processe. The rest I have already done
0
 
LVL 6

Expert Comment

by:GloomyFriar
ID: 9677438
>The code you gave me will allow the process to continue but when I kill my app all the processes that has been "Frozen" >terminate
I have forgotten you to notify about it.
It's normally.
As I said before you can not stop debugging after it's started. If the debugger process is terminated the debuggee process is terminated too.
0
 

Author Comment

by:HoratioH
ID: 9677916
aaaaaaawwww man....

That realy puts me back to the beginning :(

I need to freeze a process and let it run afterwards ... and be able to terminate my app without the other processes being killed :(

How would I do that?
0
 
LVL 5

Expert Comment

by:DeerBear
ID: 9677947
Hi,

Use SuspendThread to suspend a thread.
Determine all the threads in the process and just
suspend them. When you need to resume them,
just call ResumeThread for each of them.

HTH,

Andrew
0
 
LVL 6

Expert Comment

by:GloomyFriar
ID: 9677979
2 DeerBear:
To use SuspendThread we need the thread handle. But how we can get one?
I still have not found a good way to do it.
0
 

Author Comment

by:HoratioH
ID: 9677990
:)

Now that sound easy .... could you give me some code on how to suspend a process and get all the threads and suspend them too.

Please
0
 
LVL 5

Expert Comment

by:DeerBear
ID: 9678012
Well,

1) I said this alot of posts ago <g>
2) This has been shown to you in the previous 16 posts( <g> ) to mine

Read them better and you'll see the explainations.

If you need code, then knock here

HTH,

Andrew
0
 
LVL 6

Expert Comment

by:GloomyFriar
ID: 9678080
2 DeerBear
>I think that you may be well off suspending all the threads of the
>process, that should make it.
>The way to do this has already been shown to you.
Please tell me where?
0
 
LVL 5

Expert Comment

by:DeerBear
ID: 9678128
Oh, good boy!

Have a look at Hypoviax's message in this question on 11/2/2003 for example.
There u'll find the way to list processes.
If you look closely, you'll find the other messages as well.

HTH,

Andrew
0
 
LVL 6

Expert Comment

by:GloomyFriar
ID: 9678160
I'm talking about how to get remote process thread handlers, not about list f processes.
0
 
LVL 6

Expert Comment

by:GloomyFriar
ID: 9678240
It seems to me that I've done it!
0
 
LVL 6

Expert Comment

by:GloomyFriar
ID: 9678250
But it'll work under Win ME, 2000, XP and may be under NT with one patch.
0
 

Author Comment

by:HoratioH
ID: 9678282

I am lost :( ..........

how do I get a process and all its threads. Then get the handles for them .....
and then when I have the handles I will call Suspend ...

Please show me the way
0
 
LVL 6

Expert Comment

by:GloomyFriar
ID: 9678299
Just a moment...
I'll make the code readable and show it to you. ;-)
0
 
LVL 6

Expert Comment

by:GloomyFriar
ID: 9678371
HoratioH, may I send the code to you by e-mail?
0
 
LVL 6

Expert Comment

by:GloomyFriar
ID: 9678375
HoratioH, may I send the code to you by e-mail?
0
 

Author Comment

by:HoratioH
ID: 9678398
horatio@datalabs.co.za

Thanks :)
0
 
LVL 6

Accepted Solution

by:
GloomyFriar earned 500 total points
ID: 9678802
I've sent it to you.
0
 

Author Comment

by:HoratioH
ID: 9681490
You are a genius!!!!!

Thank you …..

ps. could you maybe answer a question I have posted that is worth 150 points:
Give me a list of all processes currently frozen in the system. External from my app.
Link:
http://www.experts-exchange.com/Programming/Programming_Languages/Delphi/Q_20785283.html
 
Again Thank you.

Could I ask you one more question. You dont have to answer it you already have the points, but.... How can I clear the message list for the threads before I ResumeThread?

Defrag takes a while to run and I would not like an our's messages to come rushing in to the threads when i resume the thread.

This could be helpfull to know.

But again thank you.

You have my email address now so please send the answer to my mail box please.




0
 
LVL 6

Expert Comment

by:GloomyFriar
ID: 9685398
How about "If you can give me this I will give you all my points that I have"?
And why "grade of A"? :(
0
 

Author Comment

by:HoratioH
ID: 9685930
grade A is the best is it not?
0
 
LVL 6

Expert Comment

by:GloomyFriar
ID: 9686051
It's look like that. Honestly speaking, I still have not read the help ;-)  It's my fault. Sorry.
0
 
LVL 6

Expert Comment

by:GloomyFriar
ID: 9686086
>Give me a list of all processes currently frozen in the system.
It's rather difficult. Whether this question be read as "List of all threads with ThreadWaitReason=ExecutionDelay ?"
0
 
LVL 6

Expert Comment

by:GloomyFriar
ID: 9688832
>How can I clear the message list for the threads before I ResumeThread?
I'll think about it...
0
 

Author Comment

by:HoratioH
ID: 9699313
:)

Thank you. You are the best help I have ever had on this site :)
Please let me know if you have an answer :)

Thank you
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now