I have this weird and inconsistent issue.
The application is built in compact framework 3.5 running on a windows mobile 6.5 device.
The screen flow of the application is somewhat like this.
MainScreen (which is always running) Screen1 Screen2 Screen3
MainsScreen -> Screen1 -> Screen2 -> Screen3 -> MainScreen
When the loop is completed and the application lands back on MainScreen, MainScreen.Activated() is called and on a condition launches automatically Screen1. The code looks something like this:
private void MainScreen_Activated(object sender, EventArgs e)
{
if (C开发者_开发知识库ondition)
{
NextScreen();
return;
}
//other code here
}
private void NextScreen()
{
Screen1 formScreen1 = new Screen1 ();
formScreen1 .Show();
}
Screen1 has also this piece of code in the load of the form:
private void Screen1_Load(object sender, System.EventArgs e)
{
if(Condition)
{
NextScreen();
}
}
private void NextScreen()
{
Screen2 formScreen2= new Screen2();
formScreen2.Show();
Close();
}
So when this condition in Screen1_Load is fulfilled the application automatically launches Screen2 and closes this.
In this particular case it happens that sometimes a control on Screen2 gets disposed after the form is created. I know this because i can actually see the control for a split second on the screen.
The issue is also inconsistent, it won't happen all the time in the exact scenario which makes me believe it's got to something with bad timing. The control that gets disposed seems to be also chosen at random, most of the times it's the same button but there were cases when a listbox from the form got disposed.
Please note that there is no code in the application that will call dispose of the control.
Any help is greatly appreciated. Thank you, hope i explained the issue well enough.
I found the cause and a solution to my issue. After much investigation i found out that the button being disposed was coinciding with this chance exception happening in debug:
A first chance exception of type 'System.ObjectDisposedException' occurred in System.Drawing.dll
A first chance exception of type 'System.ObjectDisposedException' occurred in System.Drawing.dll
I also found out that this chance exception can happen when the device is or thinks it's running out of memory causing the GC to run and try to clear memory.
I used the answer to this question Prevent Garbage Collector and also added GC.KeepAlive on the form that were suffering this issue.
The chance exception still happens in debug at the same time but i haven't been able to reproduce the issue since then.
精彩评论