gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 21:40:49 26 4
gpt4 key购买 nike

我试图在一段时间后使我的控制台应用程序崩溃(这是因为我测试了应用程序是否会在崩溃后自行启动。遵循 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com