c# - 应用程序在另一个线程中不会故意崩溃

标签 c# multithreading crash

我试图在一段时间后使我的控制台应用程序崩溃(这是因为我测试了应用程序是否会在崩溃后自行启动。遵循 this 教程)

我所拥有的是这段代码:

static class WebSocket
{
    static int Main(string[] args)
    {
        Recovery.RegisterForAutostart();
        Recovery.RegisterForRestart();
        Test.Run();

        // some more code
    }
}

public static class Recovery
{
    [Flags]
    public enum RestartRestrictions
    {
        None = 0,
        NotOnCrash = 1,
        NotOnHang = 2,
        NotOnPatch = 4,
        NotOnReboot = 8
    }

    public delegate int RecoveryDelegate(RecoveryData parameter);

    public static class ArrImports
    {
        [DllImport("kernel32.dll")]
        public static extern void ApplicationRecoveryFinished(
            bool success);

        [DllImport("kernel32.dll")]
        public static extern int ApplicationRecoveryInProgress(
            out bool canceled);

        [DllImport("kernel32.dll")]
        public static extern int GetApplicationRecoveryCallback(
            IntPtr processHandle,
            out RecoveryDelegate recoveryCallback,
            out RecoveryData parameter,
            out uint pingInterval,
            out uint flags);

        [DllImport("KERNEL32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
        public static extern int GetApplicationRestartSettings(
            IntPtr process,
            IntPtr commandLine,
            ref uint size,
            out uint flags);

        [DllImport("kernel32.dll")]
        public static extern int RegisterApplicationRecoveryCallback(
            RecoveryDelegate recoveryCallback,
            RecoveryData parameter,
            uint pingInterval,
            uint flags);

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

        [DllImport("kernel32.dll")]
        public static extern int UnregisterApplicationRecoveryCallback();

        [DllImport("kernel32.dll")]
        public static extern int UnregisterApplicationRestart();
    }

    public class RecoveryData
    {
        string currentUser;

        public RecoveryData(string who)
        {
            currentUser = who;
        }
        public string CurrentUser
        {
            get { return currentUser; }
        }
    }

    //  Restart after crash
    public static void RegisterForRestart()
    {
        // Register for automatic restart if the application was terminated for any reason.
        ArrImports.RegisterApplicationRestart("/restart",
           (int)RestartRestrictions.None);
    }

    //  Start app when PC starts
    public static void RegisterForAutostart()
    {
    #if (!DEBUG)
        RegistryKey key = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
        key.SetValue("websocket", @"c:\websocket\run.bat");
    #endif
    }

public static class Test
{
    public static void Run()
    {
        crash();
    }

    static void crash()
    {
        double crashAfter = 1.5 * 60; //  seconds
        int secondsPassed = 0;
        int waitSeconds = 1;

        Console.WriteLine("\nCrash test startet, crash will occour in " + crashAfter + " seconds");

        Timer timer = new Timer(
            delegate (object seconds) {
                secondsPassed += int.Parse(seconds.ToString());
                if (secondsPassed > crashAfter)
                {
                    Console.WriteLine("Crashing");
                    Environment.FailFast("Test - intentional crash."); // Error happens here
                }
                else
                {
                    double timeUntilCrash = (crashAfter - secondsPassed);
                    Console.WriteLine("Time until crash = " + timeUntilCrash + " seconds");
                }
            },
            waitSeconds,
            TimeSpan.FromSeconds(waitSeconds),
            TimeSpan.FromSeconds(waitSeconds));
    }
}

当需要崩溃时,我收到此消息:

Cannot evaluate expression because a thread is stopped at a point where garbage collection is impossible, possibly because the code is optimized.

未选中代码优化复选框。

我想这是因为它不在主线程中,如果是这种情况我该如何返回主线程。如果不是,原因可能是什么?

最佳答案

我根据您的代码创建了一个应用程序,发现从命令行运行该应用程序时,一切都按预期运行 - 仅在 Visual Studio 调试器中重新启动不起作用。

关于c# - 应用程序在另一个线程中不会故意崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40356778/

相关文章:

c# - 用空格替换所有不受支持的字符

c# - 来自函数的可变数量的结果

c# - 牛顿法平方根迭代

c# - 使用 Manatee.trello 获取用户列表时出现 MissingMethodException

Linux 上的 Java BlockingQueue 延迟高

c# - 如果我在写入变量时锁定,如果读取是原子的,我是否也需要在读取时锁定?

python - 在退出之前捕获 python 应用程序

multithreading - 监视器与互斥体

CUDA 样本导致机器崩溃

c# - 没有 StackTrace 或日志的 Monotouch 应用程序崩溃(从未调用析构函数)