为什么应用程序在ShowDialog方法上抛出错误“值不能为空”?

时间:2016-04-14 15:04:16

标签: c#

我花了5天时间试图解决这个问题,但我失败了。

我是c#的新手,我使用的是Windows窗体。我构建了一个由20个表单组成的应用程序。该应用程序以2 formsSplashScreenMainForm)开头,SplashScreen表单有一个计时器和ProgressBar,MainForm有10个按钮,每个按钮打开其他形式。当此应用程序运行时,首先加载SplashScreen表单,当progressBar完成时,它会检查System.txt文件是否存在,如果存在则加载MainForm表单:< / p>

SplashScreen表格:

public partial class SplashScreen : Form
{
    // This is a timer for progressBar:
    private void timer1_Tick(object sender, EventArgs e)
    {
        progressBar1.Increment(1);
        if(progressBar1.Value==100)
        {

            timer1.Stop(); //it stops when progressBar completes.
            CheckFile();    // this method will be called when progressBar completes           

        }
    }

    public void CheckFile()
    {
        if (File.Exists(@"C:\Folder1\System.txt"))
        {
            // if the file exist Main form will be opened 

            MainForm _MainForm = new MainForm();
            this.Hide();
            _MainForm.ShowDialog();
            this.Close();
        }
    }
}

以下是程序运行9小时后抛出的错误:

screenshot

问题是什么:

现在,当我运行程序时SplashScreen加载,然后打开MainForm(因为System.txt存在)并且应用程序运行良好,但是在运行了9个小时之后应用程序抛出错误“值不能为空”,它指向ShowDialog方法(如屏幕截图所示)。请注意,即使我不使用该应用程序也会发生错误(我只需按F5并等待9小时直到发生错误)。

当我将应用程序设置为首先MainForm开始(在Program.cs上)时,应用程序可以在很长时间内无误地运行,我的意思是它可以运行一天没有此错误。仅当程序以splashScreen表单(Application.Run(new SplashScreen())

开头时才会出现此错误

我试图解决这个问题:

static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new SplashScreen());
    }
}
  • 我尝试Show()方法而不是ShowDialog(),并且应用程序抛出相同的错误,但这次错误指向Application.Run(new SplashScreen())上的Programe.cs(见上文)

  • 我也尝试了MainForm _MainForm = new MainForm() { Owner = this };

  • 我也尝试了_MainForm.ShowDialog(this);

  • 我也尝试了_MainForm.ShowDialog(); Application.DoEvents();

    我尝试的所有操作都会导致相同的错误“值不能为空”且错误指向_MainForm.ShowDialog();

现在5天我一直试图解决这个问题,但我不能,每次用SplashScreen运行应用程序时,它会在9小时之后(为什么9小时?)给出这个错误。我理解变量可以是null而不是方法,那么ShowDialog如何为空?任何人都知道为什么ShowDialog在长时间运行后抛出错误?我很乐意听到其他想法如何从MainForm打开SplashScreen并保持打开很长时间?

请花5分钟帮助我,我真的很绝望,每一个小帮助都可以解决这个问题。非常感谢您的进步。

0 个答案:

没有答案
相关问题