举例. 现在我同时打开了 软件A, 软件B,
软件A 是已经做好的软件或是游戏~ 不是我自己编写的.
软件B 是我自己现在要做的软件~
如何实现 在两个软件同时打开的情况下,
关闭掉软件A, 那么软件B也会自动关闭.
关闭掉软件B, 强制结束掉软件A的进程!
举例. 现在我同时打开了 软件A, 软件B,
软件A 是已经做好的软件或是游戏~ 不是我自己编写的.
软件B 是我自己现在要做的软件~
如何实现 在两个软件同时打开的情况下,
关闭掉软件A, 那么软件B也会自动关闭.
关闭掉软件B, 强制结束掉软件A的进程!
所谓的B自动关闭,其实就是B执行结束。因此来说,在B中只要判断A是否存活。A若存活,则继续执行。A若不存活,则结束程序,即可。
建议由B来打开A,这样B可以wait A的进程。当进程结束时,B自己退出即可。
举例
using System;
using System.Diagnostics;
using System.Threading;
namespace Q185904
{
public class Worker
{
private Process m_p;
public Worker(Process p)
{
this.m_p = p;
}
// This method will be called when the thread is started.
public void DoWork()
{
while (!_shouldStop)
{
Console.WriteLine("worker thread: working...");
if (Console.KeyAvailable)
break;
}
Console.WriteLine("worker thread: terminating gracefully.");
if (!this.m_p.HasExited)
this.m_p.Kill();
}
public void RequestStop()
{
_shouldStop = true;
}
// Volatile is used as hint to the compiler that this data
// member will be accessed by multiple threads.
private volatile bool _shouldStop;
}
internal class Program
{
private static void Main(string[] args)
{
var p = Process.Start("C:\\Windows\\notepad.exe");
p.WaitForInputIdle();
var w = new Worker(p);
var t = new Thread(w.DoWork);
t.Start();
if (!p.HasExited)
p.WaitForExit();
w.RequestStop();
}
}
}