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

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



 
HoratioHAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

gmayoCommented:
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
HoratioHAuthor Commented:
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
gmayoCommented:
I've never done it, sorry!

Geoff M.
0
HTML5 and CSS3 Fundamentals

Build a website from the ground up by first learning the fundamentals of HTML5 and CSS3, the two popular programming languages used to present content online. HTML deals with fonts, colors, graphics, and hyperlinks, while CSS describes how HTML elements are to be displayed.

GloomyFriarCommented:
>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
HoratioHAuthor Commented:
exe name
0
GloomyFriarCommented:
And if the program is started 2 times? It is necessary to stop both copies?
0
GloomyFriarCommented:
Here is the way you can get process ID:
1.EnumProcesses
2.OpenProcess
3. GetModuleBaseName or
0
GloomyFriarCommented:
Ooops. Sorry, I've pressed to submit occasionly.
Wait for a while, I'm looking for decision.
0
GloomyFriarCommented:
By the way why do you want to "freeze" the process?
Is it possible, that the process have several threads?
0
HoratioHAuthor Commented:
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
GloomyFriarCommented:
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
HoratioHAuthor Commented:
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
GloomyFriarCommented:
>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
HoratioHAuthor Commented:
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
GloomyFriarCommented:
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
GloomyFriarCommented:
I'm waiting for you answer to make it clear whether to work with it on days off or not.
0
DeerBearCommented:
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
HoratioHAuthor Commented:
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
HypoviaxCommented:
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
HypoviaxCommented:
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
HoratioHAuthor Commented:
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
GloomyFriarCommented:
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
GloomyFriarCommented:
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
HoratioHAuthor Commented:
This code is great :)

I am going to test it tonight :)

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

0
HoratioHAuthor Commented:

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
GloomyFriarCommented:
>Will this work on 98/nt/2000/xp ?
I've tested it under Win2k.
0
GloomyFriarCommented:
>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
GloomyFriarCommented:
>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
HoratioHAuthor Commented:
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
HypoviaxCommented:

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
HoratioHAuthor Commented:
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
GloomyFriarCommented:
>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
HoratioHAuthor Commented:
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
DeerBearCommented:
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
GloomyFriarCommented:
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
HoratioHAuthor Commented:
:)

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
DeerBearCommented:
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
GloomyFriarCommented:
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
DeerBearCommented:
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
GloomyFriarCommented:
I'm talking about how to get remote process thread handlers, not about list f processes.
0
GloomyFriarCommented:
It seems to me that I've done it!
0
GloomyFriarCommented:
But it'll work under Win ME, 2000, XP and may be under NT with one patch.
0
HoratioHAuthor Commented:

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
GloomyFriarCommented:
Just a moment...
I'll make the code readable and show it to you. ;-)
0
GloomyFriarCommented:
HoratioH, may I send the code to you by e-mail?
0
GloomyFriarCommented:
HoratioH, may I send the code to you by e-mail?
0
HoratioHAuthor Commented:
horatio@datalabs.co.za

Thanks :)
0
GloomyFriarCommented:
I've sent it to you.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
HoratioHAuthor Commented:
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
GloomyFriarCommented:
How about "If you can give me this I will give you all my points that I have"?
And why "grade of A"? :(
0
HoratioHAuthor Commented:
grade A is the best is it not?
0
GloomyFriarCommented:
It's look like that. Honestly speaking, I still have not read the help ;-)  It's my fault. Sorry.
0
GloomyFriarCommented:
>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
GloomyFriarCommented:
>How can I clear the message list for the threads before I ResumeThread?
I'll think about it...
0
HoratioHAuthor Commented:
:)

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.