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

本文关键字:故意 崩溃 线程 另一个 应用程序 | 更新日期: 2023-09-27 18:15:56

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

我的这段代码是:

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));
    }
}

当它要崩溃的时候,我得到这样的消息:

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

未选中代码优化复选框。

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

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

我基于你的代码创建了一个应用程序&当应用程序从命令行运行时,发现一切都像预期的那样运行-只有在Visual Studio调试器中重启不起作用

多亏PaulF我们才发现了问题。我在Debug模式下进行测试,在Visual Studio之外的发布模式下运行应用程序解决了这个问题。下面的NullReferenceException是由于重新启动时缺少命令行参数引起的。