2

我有一个全局变量 Program.intReturnVal,它并不总是返回到调用应用程序。

这个特定的 catch 块是失败的。它具有与正常工作的其他代码相同的所有代码。如果这很重要, someMethod() 实际上正在调用存储过程:

            catch (Exception ex)
            {
                GlobalClass.WriteToTextFile(ErrorLogFile, "Exception Error: someMethod() failed." + Environment.NewLine + "Additional details: " + ex.ToString(), ErrorLogPath);
                Program.intReturnVal = ErrorCode;
                Form.ActiveForm.Close();
            }

此时 Program.intReturnVal 的值为 900

    private void Form1_FormClosing(object sender, FormClosedEventArgs e)
    {
        MessageBox.Show("Inside form closing block - " + Program.intReturnVal);

    }

此时 Program.intReturnVal 的值为 900

    private void Form1_FormClosed(object sender, FormClosedEventArgs e)
    {
        MessageBox.Show("Inside form closed block - " + Program.intReturnVal);

    }

此时 Program.intReturnVal 的值为 900。

现在...在 Program.cs 页面中:

static class Program
{
    public static int intReturnVal;
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static int Main()
    {
        intReturnVal = 900; // Zero is success 900 is error
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        try
        {
            Application.Run(new Form1());
        }
        catch
        {
            intReturnVal = 900;
        }
        MessageBox.Show("Inside Program.cs block " + Program.intReturnVal + " " + intReturnVal);
        return intReturnVal;
    }
}

此时 Program.intReturnVal 的值为 0。什么是重置为 0?可以在 Dispose 方法中吗?

    protected override void Dispose(bool disposing)
    {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);

    }

我应该在表单关闭和关闭方法中的参数中添加错误代码吗?这对我来说似乎很愚蠢,因为我已经看到这些点的值是 900。

TIA 寻求帮助。

4

0 回答 0