//这里可能要加一个检测语句, 判断只有一个或者两个进程运行的这个应用程序在系统中运行, /否则, 程序就会一直不断自杀起新进程, 不断重复ProcessStartInfo pi = new ProcessStartInfo(Assembly.GetExecutingAssembly().Location); Process prc = new Process(); prc.StartInfo = pi; prc.Start(); //杀掉自己这个进程 Process.GetCurrentProcess().Kill();
可以使用Timer来完成这个操作.
在.Net中提供了两个Timer.
如果应用程序是WinForm, 可以使用System.Windows.Forms.Timer.
如果不是WinForm程序, 我们可以使用System.Threading.Timer.然后, 在Timer触发的Event handler中写如下代码来起一个新的进程运行您的这个应用程序.
//这里可能要加一个检测语句, 判断只有一个或者两个进程运行的这个应用程序在系统中运行,
/否则, 程序就会一直不断自杀起新进程, 不断重复ProcessStartInfo pi = new ProcessStartInfo(Assembly.GetExecutingAssembly().Location);
Process prc = new Process();
prc.StartInfo = pi;
prc.Start();
//杀掉自己这个进程
Process.GetCurrentProcess().Kill();
Application.Restart这个更方便...
我这里怎么没有这个方法啊,我用的是.net2003,有Application.Exit
2003 就用hertcloud(·£孙子兵法£·)的方法实验下,不知道杀死自己后是否还能重起?
不行就做个外壳来控制!
不想换2005的话就用hertcloud(·£孙子兵法£·) 的方法吧
[STAThread]
static void Main()
{
new Form1().Show();
System.Windows.Forms.Application.Run();
} private void button1_Click(object sender, System.EventArgs e)
{
this.Close();
new Form1().Show();
}
Application.Restart();
Application.Exit(); 这样写也不行