调试器未捕获 C# 崩溃

发布于 2024-12-03 11:13:17 字数 1441 浏览 1 评论 0原文

我有一个相当大的项目,最近刚刚开始在退出时崩溃。我不是最伟大的 C# 程序员,但一切通常都按预期运行。然而,在这种情况下,当我关闭控制台应用程序时,它会崩溃,但 MSVC 调试器不会捕获它。

1)当我在Release中构建它并运行它时,它通常总是在退出时崩溃。

2)当我在调试中构建它并运行它时,它似乎只有每 3-5 次中的 1 次才会在退出时崩溃。

虽然当它没有正确退出时,我确实设法得到了错误,但不幸的是退出代码与我在网上找到的任何内容都不匹配。

The program '[11108] MCDaemon.vshost.exe: Managed (v4.0.30319)' has exited with code -1073741510 (0xc000013a).

从我读到的其他人的问题来看,这通常是由非托管代码起作用引起的。有没有什么方法可以让我在缺少大量调试行的情况下真正找出问题所在?

编辑

从下面的答案来看,这是导致退出代码的代码,但我真的没有看到它有任何问题。

public static Boolean Handler(MyWin32.CtrlTypes CtrlType)
{
    // A switch to handle the event type.
    switch (CtrlType)
    {
        case MyWin32.CtrlTypes.CTRL_C_EVENT:
            Program.TerminateProcess();
            break;
        case MyWin32.CtrlTypes.CTRL_CLOSE_EVENT:
            Program.TerminateProcess();
            break;
    }

    return true;
}

public static void TerminateProcess()
{
    // Stop the Poll Timer from Running
    PollTimer.Stop();

    log.LogMessage("Process is being Shutdown.");
    log.LogMessage("Requesting Process to Stop....");
    SendProcessCmd("stop");

    // Wait and make sure it has exited
    Thread.Sleep(5000);

    if (!myProcess.HasExited)
    {
        log.LogMessage("My Process did not stop on its own, forcing Process to quit.");
        myProcess.Kill();
    }

    log.LogMessage("My Process has been Shutdown.");
}

I have a pretty large project that just recently started crashing on exit. I am not the greatest C# coder but everything generally works as intended. In this case however, when I close my console application, it crashes but is not caught by the MSVC Debugger.

1) When I build it in Release and run it, it generally always crashes when I exit.

2) When I build it in Debug and run it, it seems to only crash on exit every 1 out of 3-5 times.

I did manage to get the error though when it didn't exit properly, but unfortunately the Exit Code doesn't match anything I can find online.

The program '[11108] MCDaemon.vshost.exe: Managed (v4.0.30319)' has exited with code -1073741510 (0xc000013a).

From what I have read on other people's questions, this is generally caused by Unmanaged Code acting up. Is there any way I can actually find out what the issue is short of a massive amounts of debug lines?

EDIT

From an answer below, this is the code causing the Exit code, but I don't really see anything issue with it.

public static Boolean Handler(MyWin32.CtrlTypes CtrlType)
{
    // A switch to handle the event type.
    switch (CtrlType)
    {
        case MyWin32.CtrlTypes.CTRL_C_EVENT:
            Program.TerminateProcess();
            break;
        case MyWin32.CtrlTypes.CTRL_CLOSE_EVENT:
            Program.TerminateProcess();
            break;
    }

    return true;
}

public static void TerminateProcess()
{
    // Stop the Poll Timer from Running
    PollTimer.Stop();

    log.LogMessage("Process is being Shutdown.");
    log.LogMessage("Requesting Process to Stop....");
    SendProcessCmd("stop");

    // Wait and make sure it has exited
    Thread.Sleep(5000);

    if (!myProcess.HasExited)
    {
        log.LogMessage("My Process did not stop on its own, forcing Process to quit.");
        myProcess.Kill();
    }

    log.LogMessage("My Process has been Shutdown.");
}

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

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

发布评论

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

评论(2

怂人 2024-12-10 11:13:17
// Wait and make sure it has exited
Thread.Sleep(5000);

这就是问题陈述。您在代码中 pinvoking SetConsoleCtrlHandler() 来调用您的 Handler 方法。 Windows 要求控制处理程序具有响应能力,并且从回调返回的时间不要太长。 CTRL_CLOSE_EVENT 的超时正好 5 秒,这解释了它有时有效的原因。如果需要更长的时间,Windows 就会终止该进程。

您需要以不同的方式实现 TerminateProcess。杀死一个进程是相当任意的,你最好不要这样做。或者启动另一个守护进程。但我无法拨打这个电话。

// Wait and make sure it has exited
Thread.Sleep(5000);

That's the problem statement. You are pinvoking SetConsoleCtrlHandler() in your code to get your Handler method called. Windows requires the control handler to be responsive and not take too long to return from the callback. The timeout for the CTRL_CLOSE_EVENT is exactly 5 seconds, explaining why it sometimes works. If it takes longer then Windows pulls the plug on the process.

You'll need to implement TerminateProcess differently. Killing a process is fairly arbitrary, you ought to be better off just not doing this. Or start another guard process. I can't make that call though.

追我者格杀勿论 2024-12-10 11:13:17

0xC000013A:应用程序因 Ctrl+C 而终止。

这是因为您关闭了控制台窗口,而应用程序并不期望您这样做,因此 Windows 为您终止了它。

抱歉,但我不知道如何在托管代码中捕获控制台窗口关闭事件。

0xC000013A: The application terminated as a result of a Ctrl+C.

This is because you closed the console window, and the application wasn't expecting you to, so Windows terminated it for you.

Sorry, but I don't know how to catch a console window close event in managed code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文