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:
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 ofShowDialog()
and the application throws same error but this time the error points atApplication.Run(new SplashScreen())
on thePrograme.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.