C# 退出应用程序错误

C# 退出应用程序错误,c#,C#,我在.NET4上开发了一个简单的windows应用程序。 我想向我的应用程序传递一个参数,如果该参数为空,应用程序将不会继续运行并退出 我在main方法中添加了一个输入参数。当我在Visual Studio中调试它时,它工作正常,但当我直接执行应用程序时,Windows给了我以下错误: “应用程序已停止工作” 我怎么办?我测试了一些代码,但不起作用 Application.ExitThread(); Application.Exit(); 这可能是主要的方法: [STAThread]

我在.NET4上开发了一个简单的windows应用程序。 我想向我的应用程序传递一个参数,如果该参数为空,应用程序将不会继续运行并退出

我在main方法中添加了一个输入参数。当我在Visual Studio中调试它时,它工作正常,但当我直接执行应用程序时,Windows给了我以下错误:

“应用程序已停止工作”

我怎么办?我测试了一些代码,但不起作用

 Application.ExitThread();
 Application.Exit();
这可能是主要的方法:

 [STAThread]
 static void Main(string[] args)
 {
     if(args.Count()==0)
      Thread.CurrentThread.Abort(); 

        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);

        Application.Run(new Form1());
 }
我查看了Windows事件查看器,发现此错误事件

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()
还有一个:

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

我希望这与您提供的代码一致。调用
Thread.Abort()
会在线程上引发异常(
ThreadAbortException
,该异常将被取消捕获)

如果您只想停止不带参数的处理,只需返回:

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

在数组上使用
Count()
时,只要
Length
就可以了。不过还是有一些小细节。

我希望您提供的代码能够提供这些细节。调用
Thread.Abort()
会在线程上引发异常(
ThreadAbortException
,该异常将被取消捕获)

如果您只想停止不带参数的处理,只需返回:

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

在数组上使用
Count()
时,只要
Length
就可以了。不过是小细节。

我不明白上面代码的原因

如果此参数为空,则应用程序不继续运行,并且 出口有效

您可以用这种方式轻松地编写代码

 [STAThread]
 static void Main(string[] args)
 {
     if(args.Length > 0)
     {

        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);

        Application.Run(new Form1());
     }
 }

我不明白你上面代码的原因

如果此参数为空,则应用程序不继续运行,并且 出口有效

您可以用这种方式轻松地编写代码

 [STAThread]
 static void Main(string[] args)
 {
     if(args.Length > 0)
     {

        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);

        Application.Run(new Form1());
     }
 }