Reputation:
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.");
}
Upvotes: 2
Views: 2996
Reputation: 941317
// 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.
Upvotes: 1
Reputation: 55382
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.
Upvotes: 1