在 Mono 中保留退出代码

发布于 2025-01-07 04:13:24 字数 2486 浏览 0 评论 0原文

我有一个使用 C# 编写的简单应用程序,它通过命令行参数接受操作数,并通过退出代码报告成功或失败。当通过批处理文件在 Windows XP 上运行时:

MyProg.exe ...//Snip: Command-line Params ...
echo %errorlevel%;

返回的退出代码是应用程序设置的值。但是,当使用 bash 脚本在 Linux 上调用同一应用程序时:

mono MyProg.exe ...//Snip: Command-line Params ...
echo $?;

报告给 shell 的退出代码始终为零,而不是应用程序设置的值。有什么方法可以捕获应用程序在被 Mono 修改之前设置的退出代码吗?

我尝试过使用 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();
  }
 }
}

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:

MyProg.exe ...//Snip: Command-line Params ...
echo %errorlevel%;

The exit code returned is the value set by the application. However, when the same app is invoked on Linux using a bash-script:

mono MyProg.exe ...//Snip: Command-line Params ...
echo $?;

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 before it is modified by mono?

I've tried using Environment.Exit() and Application.Exit(), there seems to be no difference with respect to this issue. Also, the application is explicitly setting Environment.ExitCode before calling the exit functions.

Here is a simple WinForms app which reproduces this behaviour:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小瓶盖 2025-01-14 04:13:24

Mono 不重写任何东西。我的测试应用程序:

pakrym@ubuntu:/home/pakrym# cat exit.cs 
class Program
{
 public static void Main()
 {
      System.Environment.Exit(1);
 }
}
pakrym@ubuntu:/home/pakrym# mono ./exit.exe 
pakrym@ubuntu:/home/pakrym# echo $?
1
pakrym@ubuntu:/home/pakrym# mono --version
Mono JIT compiler version 2.6.7 (Debian 2.6.7-5ubuntu3)
Copyright (C) 2002-2010 Novell, Inc and Contributors. www.mono-project.com
    TLS:           __thread
GC:            Included Boehm (with typed GC and Parallel Mark)
SIGSEGV:       altstack
Notifications: epoll
Architecture:  x86
Disabled:      none

Mono doesn't rewrite anything. My test app:

pakrym@ubuntu:/home/pakrym# cat exit.cs 
class Program
{
 public static void Main()
 {
      System.Environment.Exit(1);
 }
}
pakrym@ubuntu:/home/pakrym# mono ./exit.exe 
pakrym@ubuntu:/home/pakrym# echo $?
1
pakrym@ubuntu:/home/pakrym# mono --version
Mono JIT compiler version 2.6.7 (Debian 2.6.7-5ubuntu3)
Copyright (C) 2002-2010 Novell, Inc and Contributors. www.mono-project.com
    TLS:           __thread
GC:            Included Boehm (with typed GC and Parallel Mark)
SIGSEGV:       altstack
Notifications: epoll
Architecture:  x86
Disabled:      none
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文