1

I spent 5 days trying to solve this issue but I failed.

I am new to c# and I am using windows forms. I built an application which consists of 20 forms. The application starts with 2 forms (SplashScreen and MainForm) the SplashScreen form has a timer and ProgressBar and the MainForm has 10 buttons each of which opens other forms. When this application runs it first load the SplashScreen form and when the progressBar completes it checks if System.txt file exists or not, if it exists it loads the MainForm form:

SplashScreen Form:

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

Here is the error the program throws after 9 hours of running:

screenshot

What is the issue:

Now when I run the program the SplashScreen loads and then it opens the MainForm (because System.txt exists) and the application works very well, however after exactly 9 hours of running the application it throws error "Value cannot be null" and it points at ShowDialog method (as shown on screenshot). Note that the error occurs even if I don't use the application (I just press F5 and wait 9 hours till the error occurs).

When I set the application to start with the MainForm first (on Program.cs) the application works without error for very long hours, I mean it works for a days without this error. The error occurs only when the program starts with splashScreen Form (Application.Run(new SplashScreen()))

What I tried to solve the issue:

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());
    }
}
  • I tried Show() method instead of ShowDialog() and the application throws same error but this time the error points at Application.Run(new SplashScreen()) on the Programe.cs (see above).

  • Also I tried MainForm _MainForm = new MainForm() { Owner = this };

  • Also I tried _MainForm.ShowDialog(this);

  • Also I tried _MainForm.ShowDialog(); Application.DoEvents();

    All what I tried resulted in same error "Value cannot be null" and the error points at _MainForm.ShowDialog();

Now 5 days I have been trying to solve this issue but I couldn't, every time I run the application with SplashScreen it gives this error exactly after 9 hours (why 9 hours?). I understand that a variable can be null not a method, so how can ShowDialog be null ? Anyone knows why ShowDialog throws error after long runtime? I will be happy to hear other ideas in how can I open MainForm from SplashScreen and keep it open for long time?

Please take 5 minutes to help me I am really desperate, every little help could solve this issue. Thank you very much in advanced.

Shelby115
  • 2,816
  • 3
  • 36
  • 52
Kate
  • 935
  • 4
  • 14
  • 34
  • 1
    Why are you loading the mainform as a modal? The splash screen close will not get called until the modal method returns, which wont happen until the modal closes. Try just using `_mainForm.Show();` – Wjdavis5 Apr 14 '16 at 15:10
  • 1
    You seem to execute `MainForm _MainForm = new MainForm()` multiple times. You probably should not do that, just create it once, and use the same instance each time you need it again. – wimh Apr 14 '16 at 15:12
  • @ Wjdavis5 . I am new to c# I dont know what you mean by modal? I already tried using _mainForm.Show(); but it throws same error as i said. – Kate Apr 14 '16 at 15:14
  • 1
    Do you have a stack trace? This might give some insights into what's throwing the error. – amura.cxg Apr 14 '16 at 15:14
  • The problem clearly lies deeper. It's not `Show` or `ShowDialog` that causes the problem (directly). They wouldn't throw an `ArgumentNullException`. It must be thrown by some method, or constructor, that's executed within `MainForm` itself once it's initializing. Get the full stracktrace, and post the code of `MainForm` itself.. – Konrad Morawski Apr 14 '16 at 15:16
  • @ Wimmel. No, I execute MainForm _MainForm = new MainForm() only one time once the progressBar completes. I already did what you said but it throws same error ( I just created it once, and used the same instance each time) , still same error. – Kate Apr 14 '16 at 15:16
  • @amura.cxg .I am sorry I am new to c# , can you tell me more about stack trace? thank you – Kate Apr 14 '16 at 15:18
  • @Kate In the exception dialog if you click "View Details" a new window will open up. If you expand the first element there's an entry marked as "StackTrace". [Here's](http://imgur.com/a/r31n4) a few images of what that looks like. – amura.cxg Apr 14 '16 at 15:22
  • @amura.cxg . There is no "View Details" section on the exception dialog as shown in the screenshot above, any idea where to find it? tnx – Kate Apr 14 '16 at 15:30
  • We just don't have enough code from either SplashScreen or MainForm to determine what is causing your issue. – LarsTech Apr 14 '16 at 15:31
  • @ LarsTech . I can't post the MainForm Code because it is very long. The MainForm Code reads and writes data into sql server and the form has many instances of other forms to open and close, that is all the code. splashScreen code is shown above. – Kate Apr 14 '16 at 15:36
  • Then try: `Application.Run(new MainForm());this.Close();` instead. – LarsTech Apr 14 '16 at 15:42
  • Why don't you make the _MainForm static and be done with it? – Movsar Bekaev Apr 14 '16 at 16:00
  • @ Movsar Bekaev. I am sorry I am new to c#, can you tell me how can I make it static? tnx – Kate Apr 14 '16 at 16:04
  • I would guess that this is related to a timer or something like that, possibly involving a `short`, as the max value of short would be approximately 9 hours in seconds. – Kateract Apr 15 '16 at 17:01
  • This might be a GDI Object Leak, caused somewhere else in your program. Maybe you can look at [How to debug GDI Object Leaks?](http://stackoverflow.com/a/8306253/33499). [Stacktrace](http://stackoverflow.com/q/36652601/33499). @Kate, when you reply, don't add a space after the `@`! – wimh Apr 15 '16 at 17:23

0 Answers0