C# 返回form_closing和Program.cs之间的变量更改
我有一个全局变量Program.intReturnVal,它并不总是返回到调用应用程序 这个特殊的捕捉块是失败的。它与其他正常工作的代码完全相同。someMethod实际上是在调用存储过程(如果这很重要):C# 返回form_closing和Program.cs之间的变量更改,c#,.net,global-variables,C#,.net,Global Variables,我有一个全局变量Program.intReturnVal,它并不总是返回到调用应用程序 这个特殊的捕捉块是失败的。它与其他正常工作的代码完全相同。someMethod实际上是在调用存储过程(如果这很重要): catch (Exception ex) { GlobalClass.WriteToTextFile(ErrorLogFile, "Exception Error: someMethod() failed." +
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);
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
MessageBox.Show("Inside form closed block - " + Program.intReturnVal);
}
此时Program.intReturnVal的值为900
private void Form1_FormClosing(object sender, FormClosedEventArgs e)
{
MessageBox.Show("Inside form closing block - " + Program.intReturnVal);
}
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;
}
}
我是否应该在表单closing和closed方法中的参数中添加错误代码?这对我来说似乎很愚蠢,因为我已经看到这些点的值是900
TIA需要任何帮助。好吧,这至少是公共静态字段是邪恶的原因之一。将其设置为属性,在setter上设置断点,现在您可以调试它。如果我使用System.Environment.ExiterOrCode而不是Form.ActiveForm.Close,它可以工作,但不会通过Program.cs。它做了大部分我需要它做的事情,但我知道一定有更好的方法。我将研究汉斯的建议。好吧,这至少是公共静态场是邪恶的原因之一。将其设置为属性,在setter上设置断点,现在您可以调试它。如果我使用System.Environment.ExiterOrCode而不是Form.ActiveForm.Close,它可以工作,但不会通过Program.cs。它做了大部分我需要它做的事情,但我知道一定有更好的方法。我将研究汉斯的建议。