软件崩溃如何自动启动 代码使用了多线程,不知道为什么时间长了就会崩溃能否实现让软件崩溃后自动重启。。或类似的功能好像可以再用一个软件监控进程中是否有该软件进程,如果没有则可以自动启动指定路径的EXE文件现在问题是,软件打开了多次,进程中名称是相同的,可能有多个。。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 我觉得你的思路可行,只要取得当前进程列表,然后遍历它,检测如果没有目标进程,那么启动一个使用这个名称空间里的功能应该能够完成相当,System.Diagnostics Application.Restart();主程序启动一个进程 期待 UP UP UP UP 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(); } }} 1.try catch 到每个可能发生的异常2.在catch里用process类重启一个instance,并关闭当前instance。 在主线程的FormClosing事件中Application.Restart不行吗? 也可以获得form的句柄 然后通过name来判断进程是否存在 比较好的处理方式,建立一个WindowsService。参考代码如下: 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(); } } } 关于架构基础的一点问题!! Effective C# 第二版 中文翻译之01 求一句c#:如果文件"C:\333.txt"存在,则.... C#读Serialport COM1 异常 开始自学C#,一天争取帮人解决一个C#问题来提高。有问题尽管提! 紧急求救~~如果使Dialog的父窗口变成活动窗口? winfrom中。时间验证控件该如何处理! xml中怎么实现页面显示问题,100分,求问?? <<<界面登陆问题>>> 谁做过用C#和灭菌锅或者清洗机之类的,有没有参考的资料之类的 fatie 刚才发的大话设计模式地址有点问题,结了在此重开一贴
只要取得当前进程列表,然后遍历它,检测如果没有目标进程,那么启动一个
使用这个名称空间里的功能应该能够完成相当,
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();
}
}
}