代码使用了多线程,不知道为什么时间长了就会崩溃
能否实现让软件崩溃后自动重启。。或类似的功能好像可以再用一个软件监控进程中是否有该软件进程,如果没有则可以自动启动指定路径的EXE文件
现在问题是,软件打开了多次,进程中名称是相同的,可能有多个。。
能否实现让软件崩溃后自动重启。。或类似的功能好像可以再用一个软件监控进程中是否有该软件进程,如果没有则可以自动启动指定路径的EXE文件
现在问题是,软件打开了多次,进程中名称是相同的,可能有多个。。
只要取得当前进程列表,然后遍历它,检测如果没有目标进程,那么启动一个
使用这个名称空间里的功能应该能够完成相当,
System.Diagnostics
主程序启动一个进程
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;namespace WindowsFormsApplication24
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException); throw new Exception("!"); Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
} static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
// 不判断就重启程序就只有重启电脑了
if (MessageBox.Show("程序发生未处理的异常:" + Environment.NewLine
+ e.ExceptionObject.ToString() + ",是否重启?", String.Empty,
MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk) == DialogResult.OK)
Application.Restart();
}
}
}
2.在catch里用process类重启一个instance,并关闭当前instance。
public class Service1 : ServiceBase
{
private IContainer components = null;
private Process mProcess;
private bool mStop = false; public Service1()
{
this.InitializeComponent();
} protected override void Dispose(bool disposing)
{
if (disposing && (this.components != null))
{
this.components.Dispose();
}
base.Dispose(disposing);
} private void InitializeComponent()
{
this.components = new Container();
base.ServiceName = "ProcessWebMetricsService";
} private void MyWaitCallback(object state)
{
while (!this.mStop)
{
this.mProcess = Process.Start(@"D:\WebMetricsAPIData\bin\Release\WebMetricsData.exe");
this.mProcess.WaitForExit();
}
} protected override void OnStart(string[] args)
{
ThreadPool.QueueUserWorkItem(new WaitCallback(this.MyWaitCallback));
} protected override void OnStop()
{
this.mStop = true;
if (this.mProcess != null)
{
this.mProcess.Kill();
}
}
}