在 bat 文件中处理应用程序崩溃

发布于 2024-12-13 12:44:08 字数 395 浏览 1 评论 0原文

我有一个用 C# 编写的应用程序(.NET 控制台应用程序),在服务器上运行。有时(大约每周两次)应用程序崩溃,这可能是由于网络故障或类似原因引起的,但这并不重要,但是我想要的是,当应用程序崩溃时我想要它只是默默地重新启动。我无法重写应用程序来执行此操作,因为我没有源代码,并且我想要一个简单的解决方案,因此我尝试创建一个如下的 bat 文件:

:start
@BotR.TestR.exe
echo sleeping
@ping 123.45.67.89 -n 1 -w %1000 > nul
goto start

但是,每当应用程序 (BotR.TestR. exe)崩溃时,它会弹出一个弹出窗口,告诉您应用程序崩溃了,它会阻止它继续运行,从而阻止它重新启动。有什么简单的方法可以解决这个问题吗?

I have a application (.NET console app) written in C# that I run on a server. From time to time (about 2 times a week) the application crashes, this might for instance be caused by the network going down or something like that, but that's not important, what I want however, is that when the application crash I want it to simply silently restart. I can't rewrite the application to do this as I don't have the source, and I would like a simple solution to this, so I tried to create a bat file like this:

:start
@BotR.TestR.exe
echo sleeping
@ping 123.45.67.89 -n 1 -w %1000 > nul
goto start

However, whenever the application (BotR.TestR.exe) crashes, it pops up with a popup-window telling that the application crashed, witch stops it from continuing and thus stops it from restarting. Is there any simple way I can solve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一个人练习一个人 2024-12-20 12:44:08

我不确定这是最好的方法,但您可以使用 Mono Cecil 重写应用程序像这样做你想做的事:

ModuleDefinition module = ModuleDefinition.ReadModule(fileName);

var entryPoint = module.EntryPoint.Body;

var corlib = module.TypeSystem.Corlib;

var exceptionTypeReference = new TypeReference(
    "System", "Exception", null, corlib, false);
exceptionTypeReference = module.Import(exceptionTypeReference);

var entryPointIL = entryPoint.GetILProcessor();

var objectTypeReference = new TypeReference(
    "System", "Object", null, corlib, false);
objectTypeReference = module.Import(objectTypeReference);

var writeLineMethod =
    new MethodReference(
        "WriteLine",
        new TypeReference("System", "Void", null, corlib, true),
        new TypeReference("System", "Console", null, corlib, false))
    { Parameters = { new ParameterDefinition(objectTypeReference) } };
writeLineMethod = module.Import(writeLineMethod);

var callWriteLine = entryPointIL.Create(OpCodes.Call, writeLineMethod);
entryPointIL.Append(callWriteLine);

entryPointIL.Emit(OpCodes.Br, entryPoint.Instructions.First());

var exceptionHandler = new ExceptionHandler(ExceptionHandlerType.Catch)
                        {
                            CatchType = exceptionTypeReference,
                            TryStart = entryPoint.Instructions.First(),
                            TryEnd = callWriteLine,
                            HandlerStart = callWriteLine,
                            HandlerEnd = entryPoint.Instructions.Last()
                        };
entryPoint.ExceptionHandlers.Add(exceptionHandler);

module.Write(fileName);

这段代码的作用是获取应用程序的入口点(通常是 Main() 方法)并将其从这样重写:

static void Main()
{
    // some code
}

到这样:

static void Main()
{
    while (true)
    {
        try
        {
            // some code
        }
        catch (Exception exception)
        {
            Console.WriteLine(exception);
        }
    }
}

编辑:< /strong>

如果Main() 方法和包含类是公共的,有一种更简单的方法:创建一个应用程序,将另一个方法作为引用并运行其 Main()。像这样的东西:

class Program
{
    void Main()
    {
        while (true)
        {
            try
            {
                OtherApplication.Program.Main();
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception);
            }
        }
    }
}

I'm not sure this is the best way, but you can rewrite the application using Mono Cecil to do what you want like this:

ModuleDefinition module = ModuleDefinition.ReadModule(fileName);

var entryPoint = module.EntryPoint.Body;

var corlib = module.TypeSystem.Corlib;

var exceptionTypeReference = new TypeReference(
    "System", "Exception", null, corlib, false);
exceptionTypeReference = module.Import(exceptionTypeReference);

var entryPointIL = entryPoint.GetILProcessor();

var objectTypeReference = new TypeReference(
    "System", "Object", null, corlib, false);
objectTypeReference = module.Import(objectTypeReference);

var writeLineMethod =
    new MethodReference(
        "WriteLine",
        new TypeReference("System", "Void", null, corlib, true),
        new TypeReference("System", "Console", null, corlib, false))
    { Parameters = { new ParameterDefinition(objectTypeReference) } };
writeLineMethod = module.Import(writeLineMethod);

var callWriteLine = entryPointIL.Create(OpCodes.Call, writeLineMethod);
entryPointIL.Append(callWriteLine);

entryPointIL.Emit(OpCodes.Br, entryPoint.Instructions.First());

var exceptionHandler = new ExceptionHandler(ExceptionHandlerType.Catch)
                        {
                            CatchType = exceptionTypeReference,
                            TryStart = entryPoint.Instructions.First(),
                            TryEnd = callWriteLine,
                            HandlerStart = callWriteLine,
                            HandlerEnd = entryPoint.Instructions.Last()
                        };
entryPoint.ExceptionHandlers.Add(exceptionHandler);

module.Write(fileName);

What this code does is to take the entry point of the application (usually the Main() method) and rewrite it from this:

static void Main()
{
    // some code
}

into this:

static void Main()
{
    while (true)
    {
        try
        {
            // some code
        }
        catch (Exception exception)
        {
            Console.WriteLine(exception);
        }
    }
}

EDIT:

If the Main() method and the contain class are public, there is an easier way: create an application that has the other one as a reference and runs its Main(). Something like this:

class Program
{
    void Main()
    {
        while (true)
        {
            try
            {
                OtherApplication.Program.Main();
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception);
            }
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文