473,387 Members | 1,516 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How to pause/suspend a process?

Wim
My GUI application starts a process (a console program) when the user
hits Play. I would like to add an option to pause that process. The
code I've added to detect if the user hit pause/unpause seems to work.
But I have no clue how to pause/suspend the process. As far as I can
see the Process class doesn't offer anything for this. So it's probably
the thread the process is running on that should be suspended or put to
sleep. But just putting Thread.Suspend() on the place of the
questionmarks results in a compiling error (error CS0120: An object
reference is required for the nonstatic field, method, or property
'System.Threading.Thread.Suspend()'). So my question is: if I'm on the
right way how to get an object reference to the thread myProcess is
running on. Or else maybe some suggestions how to do it.

Relevant code:

private void menuPlay_Click(object sender, System.EventArgs e)
{
//(...)
MethodInvoker mi = new MethodInvoker(Execute);
mi.BeginInvoke(null, null);
}

private void Execute()
{
//(...)
myProcess.StartInfo.CreateNoWindow = true;
myProcess.Start();
while (! myProcess.HasExited)
{
Thread.Sleep(500);
if (PauseRequested == true)
{
//???
}
}
}

private bool IsPaused;

private void menuPause_Click(object sender, System.EventArgs e)
{
if (menuPause.Checked == true)
menuPause.Checked = false;
else
menuPause.Checked = true;

lock(this)
{
IsPaused = menuPause.Checked;
}
}

protected bool PauseRequested
{
get
{
lock(this)
{
return IsPaused;
}
}
}

--
Wim
Nov 15 '05 #1
8 36368
Hi Wim,

Yes your are going in the right direction. You need to suspend the current thread. This can be done by

Thread.CurrentThread.Suspend();

When you need to continue you would call the resume method.

i.e. Thread.CurrentThread.Resume();

thanks
Nithin P V

Nov 15 '05 #2
Wim
Nithin wrote:
Yes your are going in the right direction. You need to suspend the
current thread. This can be done by

Thread.CurrentThread.Suspend();

When you need to continue you would call the resume method.

i.e. Thread.CurrentThread.Resume();


That would be great, but it doesn't work: the process doesn't pause
when I hit Pause.

--
Wim
Nov 15 '05 #3

"Nithin" <an*******@discussions.microsoft.com> a écrit dans le message de
news:1F**********************************@microsof t.com...
Hi Wim,

Yes your are going in the right direction. You need to suspend the current thread. This can be done by
Thread.CurrentThread.Suspend();

When you need to continue you would call the resume method.

i.e. Thread.CurrentThread.Resume();
This part won't work. If the current thread is suspended, it won't be able
to execute the Resume method!!! A thread can only be resumed by another
thread.

Suspend and Resume are dangerous methods because you are likely to get
deadlocks if you suspend a thread that has acquired locks. IMO, it should be
reserved to special tools like debuggers and the likes.

Rather than use Suspend/Resume, I suggest that you investigate Wait / Pulse.
This is the right way to do synchronization.

Bruno.

thanks
Nithin P V

Nov 15 '05 #4
Sounds like a job for waithandles (more specifically, the ManualResetEvent):

http://www.codeproject.com/csharp/eventsthreadsync.asp

http://msdn.microsoft.com/library/de...ClassTopic.asp

http://philiprieck.com/blog/archive/...dleSample.aspx

Or the monitor class:

http://www.c-sharpcorner.com/Code/20...hreadingP3.asp

http://msdn.microsoft.com/library/de...classtopic.asp

"Wim" <wi*@nospam.invalid> wrote in message
news:Xn******************************@213.51.144.3 6...
My GUI application starts a process (a console program) when the user
hits Play. I would like to add an option to pause that process. The
code I've added to detect if the user hit pause/unpause seems to work.
But I have no clue how to pause/suspend the process. As far as I can
see the Process class doesn't offer anything for this. So it's probably
the thread the process is running on that should be suspended or put to
sleep. But just putting Thread.Suspend() on the place of the
questionmarks results in a compiling error (error CS0120: An object
reference is required for the nonstatic field, method, or property
'System.Threading.Thread.Suspend()'). So my question is: if I'm on the
right way how to get an object reference to the thread myProcess is
running on. Or else maybe some suggestions how to do it.

Relevant code:

private void menuPlay_Click(object sender, System.EventArgs e)
{
//(...)
MethodInvoker mi = new MethodInvoker(Execute);
mi.BeginInvoke(null, null);
}

private void Execute()
{
//(...)
myProcess.StartInfo.CreateNoWindow = true;
myProcess.Start();
while (! myProcess.HasExited)
{
Thread.Sleep(500);
if (PauseRequested == true)
{
//???
}
}
}

private bool IsPaused;

private void menuPause_Click(object sender, System.EventArgs e)
{
if (menuPause.Checked == true)
menuPause.Checked = false;
else
menuPause.Checked = true;

lock(this)
{
IsPaused = menuPause.Checked;
}
}

protected bool PauseRequested
{
get
{
lock(this)
{
return IsPaused;
}
}
}

--
Wim

Nov 15 '05 #5
You can't "pause" or "resume" another process, unless the other process
run's as a service.

Willy.

"Wim" <wi*@nospam.invalid> wrote in message
news:Xn******************************@213.51.144.3 6...
My GUI application starts a process (a console program) when the user
hits Play. I would like to add an option to pause that process. The
code I've added to detect if the user hit pause/unpause seems to work.
But I have no clue how to pause/suspend the process. As far as I can
see the Process class doesn't offer anything for this. So it's probably
the thread the process is running on that should be suspended or put to
sleep. But just putting Thread.Suspend() on the place of the
questionmarks results in a compiling error (error CS0120: An object
reference is required for the nonstatic field, method, or property
'System.Threading.Thread.Suspend()'). So my question is: if I'm on the
right way how to get an object reference to the thread myProcess is
running on. Or else maybe some suggestions how to do it.

Relevant code:

private void menuPlay_Click(object sender, System.EventArgs e)
{
//(...)
MethodInvoker mi = new MethodInvoker(Execute);
mi.BeginInvoke(null, null);
}

private void Execute()
{
//(...)
myProcess.StartInfo.CreateNoWindow = true;
myProcess.Start();
while (! myProcess.HasExited)
{
Thread.Sleep(500);
if (PauseRequested == true)
{
//???
}
}
}

private bool IsPaused;

private void menuPause_Click(object sender, System.EventArgs e)
{
if (menuPause.Checked == true)
menuPause.Checked = false;
else
menuPause.Checked = true;

lock(this)
{
IsPaused = menuPause.Checked;
}
}

protected bool PauseRequested
{
get
{
lock(this)
{
return IsPaused;
}
}
}

--
Wim

Nov 15 '05 #6
Create Two major threads:

a) Control Thread (Thread A)
b) Worker Thread(Thread B) (anything multithrading you do thereafter
are spawned off by this thread

c) Create a ManualResetEvent, Mutex or anything that allows for thread
synchronization to occur.
pseudo code - Assumes you have an interface
Thread A - Create Control Form We will be running in, pause / run
button on the control form. Containing Start / Pause / Stop buttons

Thread A - Create a new Thread (Thread B)
Thread A - ManualResetEventObject.Reset()
Thread A - Start Thread B
Thread B - Start Execution of a worker function
[ Worker Function]
WaitOne(ManualResetEventObject);
Do the job

- Two methods here once graceful another one is dirty - dependes on
what you are doing. If you have to pause immideately - use dirty
method, however, if you are executing someting in a loop and you can
wait to complete current cycle - use graceful method.

When Thread A creates Thread B, thread B starts the function, but the
first statement of it is WaitOne(ManualResetEventObject). For the
thread to coninue it has to wait until ManualResetEventObject gets
into a signaled state. Until then it will sleep.

Hit Play Button, as an event for Play Button Thread A executes
ManualResetEvent.Set; as soon as it happens Thread B is awokes and
starts doing its work. If you need to pause, Hit stop button; during
which Thread A will call ManualResetEventObject.Reset(); Thread B will
continue execution until the time it gets to
WaitOne(ManualResetEventObject) statement, at which point (we are in
the same position as we were when we started thread B; it will fall
asleep until ManualResetEventObject will become signaled again.

This is for a nice graceful way. If you are not doing things in a loop
and have no way to inject ManualResetEventObject or need to thread to
suspend immedeately - Let Thread A call "Thread B.Suspend()" on pause,
and "Thread B.Resume()" on resume. When you will want to terminate
thread B.

A few notes Never, ever let Thread B have a higher priority then
Thread A - might have problems suspending things:)

Dont work with processes, use threads.

Disclaimer: for the past 8 months I was working with ASP.NET thus have
not had a chance to do a lot of multithreading :)- use above for what
its worth
Wim <wi*@nospam.invalid> wrote in message news:<Xn******************************@213.51.144. 36>...
My GUI application starts a process (a console program) when the user
hits Play. I would like to add an option to pause that process. The
code I've added to detect if the user hit pause/unpause seems to work.
But I have no clue how to pause/suspend the process. As far as I can
see the Process class doesn't offer anything for this. So it's probably
the thread the process is running on that should be suspended or put to
sleep. But just putting Thread.Suspend() on the place of the
questionmarks results in a compiling error (error CS0120: An object
reference is required for the nonstatic field, method, or property
'System.Threading.Thread.Suspend()'). So my question is: if I'm on the
right way how to get an object reference to the thread myProcess is
running on. Or else maybe some suggestions how to do it.

Relevant code:

private void menuPlay_Click(object sender, System.EventArgs e)
{
//(...)
MethodInvoker mi = new MethodInvoker(Execute);
mi.BeginInvoke(null, null);
}

private void Execute()
{
//(...)
myProcess.StartInfo.CreateNoWindow = true;
myProcess.Start();
while (! myProcess.HasExited)
{
Thread.Sleep(500);
if (PauseRequested == true)
{
//???
}
}
}

private bool IsPaused;

private void menuPause_Click(object sender, System.EventArgs e)
{
if (menuPause.Checked == true)
menuPause.Checked = false;
else
menuPause.Checked = true;

lock(this)
{
IsPaused = menuPause.Checked;
}
}

protected bool PauseRequested
{
get
{
lock(this)
{
return IsPaused;
}
}
}

Nov 15 '05 #7
Did you actually read OP's question? He want's to pause the OTHER process
(a console application, probably something not written by himself). This is
something you can't do (unless you are attaching a debugger to that
process).

Willy.
Nov 15 '05 #8
Wim
"Willy Denoyette [MVP]" wrote:
Did you actually read OP's question? He want's to pause the OTHER
process (a console application, probably something not written by
himself). This is something you can't do (unless you are attaching a
debugger to that process).


Yes, you are right, that's exactly what I want to do. And I still
haven't figured it out. I don't see why it can't be done. With a
program like Process Explorer
(http://www.sysinternals.com/ntw2k/fr.../procexp.shtml) every process can
be suspended and resumed. Of course I can't program anywhere near as
good as the guys at Sysinternals, but why can't it be done from my
program?

--
Wim
Nov 15 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Lei Guangfu | last post by:
Hi, I have a program need to check the value of a application available, and then decide whether to execute codes followed. In order to achieve this purpose, I use DO WHILE loop to judge the...
11
by: Paminu | last post by:
Is there something like system("PAUSE") for linux?
1
by: Jorge | last post by:
Hi Noor If your process IS a service then yes you suspend or resume it. Kind Regards Jorge
1
by: OBINT | last post by:
Hi I am using one API to do some process for me, but unfortunately API dont have a option of pause the process and resume it back. How can i do this using C# API take more then 2 hours in...
0
by: dr | last post by:
I am trying to add suspend mode support to my application. I am getting the PBT_APMQUERYSUSPEND and PBT_APMSUSPEND event notifications as expected. According to MSDN "The system allows...
2
by: Alan T | last post by:
As the Suspend method is deprecated, what method can I use? What I want to do is when the user press the button "Cancel", it will prompt the user to confirm it he want to stop the process, I need...
3
by: =?Utf-8?B?TWFyayBDaGFubmluZw==?= | last post by:
I have a code which registers all threads with a thread dump class. At intervals this thread dump class will dump the stack trace of all threads. As calling StackTrace(threadtoDump) from a...
1
by: yxq | last post by:
How to suspend a process like Process Explorer? thank you.
3
by: =?Utf-8?B?WVhR?= | last post by:
I want to suspend/resume a process using VB.NET, how to do? Thank you
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.