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

时间:2016-11-01 08:55:26

标签: 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));
    }
}

到了崩溃的时候,我收到了这条消息:

  

无法计算表达式,因为线程在某个点停止   垃圾收集是不可能的,可能是因为代码是   优化

未选中代码优化复选框。

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

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

感谢PaulF我们发现了问题。我在Debug模式下进行测试,在Visual Studio外部以发布模式运行应用程序修复了问题。以下NullReferenceException是由重启时缺少命令行参数引起的。