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