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;
}
}
}