2
我有一个使用C#编写的简单应用程序,它通过命令行参数接受操作数,并通过退出代码报告成功或失败。在Windows XP上通过批处理文件运行时:在Mono中保留退出代码
MyProg.exe ...//Snip: Command-line Params ...
echo %errorlevel%;
返回的退出代码是应用程序设置的值。然而,当同样的应用程序在Linux上使用的bash脚本调用:
mono MyProg.exe ...//Snip: Command-line Params ...
echo $?;
报壳退出代码始终是零,而不是由应用程序设置的值。有没有办法在之前捕获应用程序设置的退出代码?
我试过使用Environment.Exit()
和Application.Exit()
,这个问题似乎没有什么区别。另外,在调用退出函数之前,应用程序明确地设置了Environment.ExitCode
。
下面是一个简单的WinForms应用程序抄录此行为:
using System;
using System.Drawing;
using System.Windows.Forms;
namespace ConsoleTest
{
public class Form1 : Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
public Form1()
{
#region Create UI Stuff
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(114, 22);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 0;
this.button1.Text = "ExitZero";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(114, 70);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(75, 23);
this.button2.TabIndex = 0;
this.button2.Text = "ExitOne";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(310, 136);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
#endregion
}
private void button1_Click(object sender, EventArgs e)
{
Environment.ExitCode = 0;
//Environment.Exit(0);
Application.Exit();
}
private void button2_Click(object sender, EventArgs e)
{
Environment.ExitCode = 1;
//Environment.Exit(1);
Application.Exit();
}
}
}
我在一个相当老的单声道版本,执行'mono --version'产量:'单声道JIT编译器版本2.0.1(tarball)'。另外,该脚本以“mono MyProg.exe”而不是“mono。/ MyApp.exe”调用该应用程序。需要进一步研究这两种差异,看看其中之一是否是原因。 – 2012-02-21 06:30:50
你能升级吗? – 2012-02-21 06:32:23
它不是不可能的,但除非没有其他解决方法可用,否则我宁愿不升级。 – 2012-02-21 06:35:15