发生异常时如何重启控制台应用程序和Windows服务 [英] How can restart console application and windows service when exception occur

查看:216
本文介绍了发生异常时如何重启控制台应用程序和Windows服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在发生异常时重新启动控制台应用程序和Windows服务



我尝试过:



如何在发生异常时重新启动控制台应用程序和Windows服务

how can Restart console application and windows service when exception occur

What I have tried:

how can Restart console application and windows service when exception occur

推荐答案

当您的应用程序终止时,它只能通过某种方式重新启动观察者。



最简单的解决方案是使用批处理/命令文件,在返回特定代码时重新启动应用程序。例如,从主函数返回零并调用 Environment.Exit Method(Int32)(System) [ ^ ]来自终止应用程序的异常处理程序中的异常特定值(可能还需要为未处理的异常提供处理程序)。



命令文件可能看起来像:

When your application has terminated it can be only restarted by some kind of watcher.

The simplest solution would be using a batch / command file that re-starts the application when it returns specific codes. For example return zero from your main function and call Environment.Exit Method (Int32) (System)[^] with an exception specific value from within exception handlers that terminate the application (it might be also necessary to provide a handler for unhandled exceptions).

The command file might look like:
@ECHO OFF
:Restart
myapp.exe
IF %ERRORLEVEL% NEQ 0 GOTO Restart



这将在myapp.exe返回非零时重启。



测试 ERRORLEVEL 变量可用于检查不同的代码。要仅检查零,命令文件可能更简单:


This will restart myapp.exe when it returns non zero.

Testing the ERRORLEVEL variable can be used to check for different codes. To check only for zero the command file might be simpler:

@ECHO OFF
:Restart
myapp.exe || GOTO Restart


这篇关于发生异常时如何重启控制台应用程序和Windows服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆