Reputation: 5635
I developed a simple windows application on .NET 4. I'd like to pass a parameter to my application and if this parameter was empty the application should not continue running and quit.
I add an input parameter in the main method. It works fine when I debug it in my Visual Studio but when I execute my application directly Windows gave me this error:
"application has stopped working"
How can i handle it? I tested some codes but it doesn't work
Application.ExitThread();
Application.Exit();
This is may main method:
[STAThread]
static void Main(string[] args)
{
if(args.Count()==0)
Thread.CurrentThread.Abort();
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
I looked at Windows Event Viewer and found this error events
Application: WindowsFormsApplication1.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.Threading.ThreadAbortException
Stack:
at System.Threading.Thread.AbortInternal()
at System.Threading.Thread.Abort()
at WindowsFormsApplication1.Program.Main()
and another:
Faulting application name: WindowsFormsApplication1.exe, version: 1.0.0.0, time
stamp: 0x50e775e3
Faulting module name: KERNELBASE.dll, version: 6.1.7601.17514, time stamp: 0x4ce7c78c
Exception code: 0xe0434352
Fault offset: 0x000000000000a49d
Faulting process id: 0x15b4
Faulting application start time: 0x01cdeade1bdab018
Faulting application path: C:\WindowsFormsApplication1\WindowsFormsApplication1
\bin\Debug\WindowsFormsApplication1.exe
Faulting module path: C:\Windows\system32\KERNELBASE.dll
Report Id: 5a6d1a1f-56d1-11e2-8ffd-20cf30e7bd24
Upvotes: 0
Views: 819
Reputation: 216243
I don't get the reason for your code above.
if this parameter was empty application do not continue running and exit works.
You could easily code in this way
[STAThread]
static void Main(string[] args)
{
if(args.Length > 0)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
Upvotes: 2
Reputation: 29668
I would expect this with the code you have supplied. Calling Thread.Abort()
raises an exception on the thread (ThreadAbortException
, which will be uncaught).
If all you want to do is stop processing with no arguments, simply return:
[STAThread]
static void Main(string[] args)
{
if(args.Length ==0) return; // Will leave and end the application
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
You also are using Count()
on the array when Length
will simply do. Minor detail though.
Upvotes: 4