简体   繁体   English

永久运行的可执行文件

[英]Permanently running executable

I created a winform (monitoring) application using VS 2005 (c#), and now, I have a problem when this application crashes for some reason, I have to be sure that it will be restarted automatically. 我使用VS 2005(c#)创建了一个winform(监视)应用程序,现在,由于某种原因该应用程序崩溃时,我遇到了一个问题,我必须确保它将自动重新启动。

How can I resolve this? 我该如何解决? (maybe by using windows services application?) (也许通过使用Windows Services应用程序?)

Thanks 谢谢

Yes, a creating a Windows Service would work as you can set it to automatically restart if it crashes but a better way would be to prevent it crashing in the first place! 是的,创建Windows服务可以正常工作,因为您可以将其设置为在崩溃时自动重新启动,但是更好的方法是首先防止它崩溃! Is there a specific reason it crashes? 崩溃有特定原因吗?

With good error handling and reporting you can write it so that it simply reports any errors that occur and carries on, which IMHO would be the best route to go 通过良好的错误处理和报告,您可以编写它,以便它仅报告发生并继续发生的任何错误,而恕我直言,这将是最好的选择

Consider this: 考虑一下:

http://msdn.microsoft.com/en-us/library/cc303699.aspx http://msdn.microsoft.com/en-us/library/cc303699.aspx

[DllImport("kernel32.dll")]
public static extern int RegisterApplicationRestart(
    [MarshalAs(UnmanagedType.BStr)] string commandLineArgs,
    int flags);

Minimum supported server 最低支持的服务器

Windows Server 2008 Windows Server 2008

http://msdn.microsoft.com/en-us/library/aa373347(VS.85).aspx http://msdn.microsoft.com/zh-CN/library/aa373347(VS.85).aspx

Creating a Windows service is a very good idea for any long-running background process for many reasons, however re-starting a crashed application is not one of them! 对于任何长时间运行的后台进程来说,创建Windows服务都是一个好主意,原因很多, 但是重启崩溃的应用程序并不是其中之一!

You should work out why the application is crashing and prevent it from happening. 您应该弄清楚应用程序崩溃的原因并防止它发生。

By all means, also convert your application to a Windows service - you will see many benefits, however the correct way to solve your problem is to fix the application crash in the first place. 无论如何,还将您的应用程序转换为Windows服务-您将看到很多好处,但是解决问题的正确方法是首先解决应用程序崩溃的问题。

For* strong text * a watcher app. 对于* 强文本 *一个观察程序。 You should create a timer on the windows service and code something like this in the timer tick event: 您应该在Windows服务上创建一个计时器,并在计时器滴答事件中编写如下代码:

        Process[] procs = Process.GetProcessesByName("you app name");
        if (procs.Length == 0)
            Process.Start("your app filename");

if you really cant do anything about the crash problem i would recommend a try-catch instead of a watcher. 如果您真的无法解决崩溃问题,我建议您使用try-catch而不是watcher。 (Dont forget to re-throw handled major exceptions) (不要忘记重新抛出已处理的主要异常)

    [STAThread]
    static void Main()
    {
        try
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
        catch(Exception ex)
        {
            //log the exception here
            Application.Restart();
        }
    }

Since you say that you use a Windows Forms application you cannot use a Windows Service for that, since a Windows Service is not allowed to have a GUI. 由于您说使用的是Windows Forms应用程序,因此不能使用Windows Service,因为不允许Windows Service具有GUI。

What I would do it that I would create an invisible "watchdog" application which monitors the process and automatically restarts it when it crashes. 我要做的就是创建一个不可见的“看门狗”应用程序,该应用程序监视进程并在崩溃时自动重新启动它。

You can put a try catch block around the code that is most likely causing the crash. 您可以在最有可能导致崩溃的代码周围放置try catch块。 Then write the exception message to a log file. 然后将异常消息写入日志文件。 You can also set a debug point in the catch block to see other details like call stack, etc. 您还可以在catch块中设置调试点,以查看其他详细信息,例如调用堆栈等。

Thanks you all, the solution I choose is : in the main program I add an exception events (UnhandledExceptionEventHandler & ThreadExceptionEventHandler see above) in these events I restart the program (also putting log & email to trace errors). 谢谢大家,我选择的解决方案是:在主程序中,我添加一个异常事件(UnhandledExceptionEventHandler和ThreadExceptionEventHandler参见上文),在这些事件中,我重新启动程序(还将日志和电子邮件用于跟踪错误)。 And for the reboot problem I add registry key in [HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run] with my application path to be sure that my application will be restarted after the windows reboot ;) 对于重新启动问题,我在[HKEY_LOCAL_MACHINE \\ SOFTWARE \\ Microsoft \\ Windows \\ CurrentVersion \\ Run]中添加注册表项,并添加我的应用程序路径,以确保Windows重新启动后,我的应用程序将重新启动;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM