title: Preserve Exit Code in Mono
body: <p>I have a simple application written using C# which accepts a operands via command-line parameters and reports success or failure via an exit code. When run on Windows XP via a batch file:</p>
  
  <pre><code>MyProg.exe ...//Snip: Command-line Params ...
  echo %errorlevel%;
  </code></pre>
  
  <p>The exit code returned is the value set by the application. However, when the same app is invoked on Linux using a bash-script:</p>
  
  <pre><code>mono MyProg.exe ...//Snip: Command-line Params ...
  echo $?;
  </code></pre>
  
  <p>The exit code reported to the shell is always zero and not the value set by the app. Is there any way to capture the exit code set by the application <em>before</em> it is modified by mono?</p>
  
  <p>I've tried using <code>Environment.Exit()</code> and <code>Application.Exit()</code>, there seems to be no difference with respect to this issue. Also, the application is explicitly setting <code>Environment.ExitCode</code> before calling the exit functions.</p>
  
  <p>Here is a simple WinForms app which reproduces this behaviour:</p>
  
  <pre><code>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();
    }
   }
  }
  </code></pre>