Reputation: 223
I have written a BAT file to run my program ten times. But at some point, the program will crash. I hope I can detect this condition then close the program. The c# process of Responding that failed to know this problem.
Upvotes: 0
Views: 704
Reputation: 151730
See How do I specify the exit code of a console application in .NET?, MSDN: Main() Return Values (C# Programming Guide) and .NET Global exception handler in console application.
The dialog is shown because you don't catch an exception. You need to combine everything shown there, so:
class Program {
static void Main(string[] args) {
AppDomain.CurrentDomain.UnhandledException += UnhandledExceptionTrapper;
// Your code here
throw new Exception("Kaboom");
}
static void UnhandledExceptionTrapper(object sender, UnhandledExceptionEventArgs e) {
Console.WriteLine(e.ExceptionObject.ToString());
Environment.Exit(1);
}
}
And:
@echo off
TestExceptionApp
@if "%ERRORLEVEL%" == "0" goto good
:fail
echo Execution Failed
echo return value = %ERRORLEVEL%
goto end
:good
echo Execution succeeded
echo Return value = %ERRORLEVEL%
goto end
:end
Upvotes: 2
Reputation: 47
You'll have to write a separate program that creates a new process that catches the error. When the catch is made you can send notification to yourself in the catch statement before the error handling notification.
It will likely need to run as a separate program called from the local user's system.
Upvotes: 0