Connecting Tech Pros Worldwide Forums | Help | Site Map

StackOverflow followed by a lot of closing forms

Member
 
Join Date: Oct 2008
Posts: 50
#1: Dec 29 '08
Hello,

Currently I'm developing an application for a Windows CE 4.0 device. This application contains a lot of different forms. The application jumps from one form to another. And I close the forms when I go to another form but I don't think it's the best way to close them.
Sometimes, at random moments, I receive a StackOverflowException and on the device I see a lot of forms closing, you see the behavior that you normally get when you form a close. So I guess it's because there are still too many forms open to manage.

When I go from one form to another I do it like this.
this.close();
Form1 frm1 = new Form1();
frm1.ShowDialog();

The problem is that the current form isn't closed right away. It's still there somewhere..Only when the new form is closed the older form is closed..

It feels like this is a stupid problem, sounds easy to fix but I can't think of a fix right away. Is there a proper way of doing form management withing a Windows CE application.

For the record, I'm developing using .NET1.1

Kind regards,

markmcgookin's Avatar
Moderator
 
Join Date: Dec 2006
Location: Northern Ireland / England
Posts: 546
#2: Dec 29 '08

re: StackOverflow followed by a lot of closing forms


It looks like you are creating a child form belonging to the parent form and then trying to close the parent form. But this isn't really happy as there is obviously a child attached (i.e. what would happen if on the child you wrote
Expand|Select|Wrap|Line Numbers
  1. this.parent;
)

You need to consider how your application is structured. Like running the main program on a thread, then launching the forms off that. Then you can close forms and create new forms off the thread and not each other.

Unfortunately I am not saying that this is the best way to do it, it's just a suggestion. You will probably find that when you are "closing" your forms they are still allocated memory and resources until you close all the child forms belonging to them and won't be disposed properly until they have no dependencies.
Member
 
Join Date: Oct 2008
Posts: 50
#3: Dec 29 '08

re: StackOverflow followed by a lot of closing forms


I think that I what you mean, I don't however not know a correct solution to this problem..

The problem is that work from one form is linked to another form. In App I launch my first form.
Application.Run(new FormUserLogin());

From that form It goes on, then user sees like an option screen where it has different options. if i select option one I open FormOption1 for instance and so on...

So how would I fix this issue?
Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,161
#4: Dec 29 '08

re: StackOverflow followed by a lot of closing forms


I would consider having Application.Run() a 'controler' form if you will.
The controller form is in charge of invoking the opening/closing of forms.
So when it starts, it will run the FormUserLogin form. Based on what the user has input on that form, the controller form closes it(or the form closes itself and the controller form can see that, whatever) and then opens up the next form.

Your form structure should then be a single parent node with a lot of leaf nodes, not a full tree structure.
Like:
Controler
/ | \ \
Login Option1 Etc Etc

Instead of doing this (bad for forms):
Login
| \
GoodLogin BadLogin
| \
Option1 Option2
|
NextOption
Member
 
Join Date: Oct 2008
Posts: 50
#5: Dec 30 '08

re: StackOverflow followed by a lot of closing forms


Is there no other way of solving the issue? The forms are nested so deep within each other that I have no clue how to solve it using the above way.
Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,161
#6: Dec 30 '08

re: StackOverflow followed by a lot of closing forms


Quote:

Originally Posted by SvenV View Post

The forms are nested so deep within each other

That should be your first clue that you have a serious design flaw.

There mayb be another way, declaring all their variables as static maybe?
And calling GC.Collect() ?
Member
 
Join Date: Oct 2008
Posts: 50
#7: Dec 30 '08

re: StackOverflow followed by a lot of closing forms


Yes I know, but the mess was there before I started working on it ;-)
GC.Collect() is maybe an idea, but I'm aware of the fact that I need to set this right or it will keep happening. The forms will stay open unless the parent is closed.
RedSon's Avatar
Site Moderator
 
Join Date: Jan 2007
Location: America
Posts: 3,392
#8: Dec 30 '08

re: StackOverflow followed by a lot of closing forms


Then its a perfect time to refactor the code. Don't be afraid of shaking things up if you inherit crappy code. Somethings are abortions when they were first started, but that doesn't mean they need to continue being that way.

Plus you should upgrade to a later version of CE. 4.0 is very limited.
Member
 
Join Date: Oct 2008
Posts: 50
#9: Dec 30 '08

re: StackOverflow followed by a lot of closing forms


Yes i know :)
But you know how it goes, time&money..
This is something however something that needs to be addressed ASAP.

I tried opening the solution in VS2005 & VS2008 but the forms came out all wacky. I recreated every form and then it kinda worked but then there was an issue with the localised resources.
RedSon's Avatar
Site Moderator
 
Join Date: Jan 2007
Location: America
Posts: 3,392
#10: Dec 30 '08

re: StackOverflow followed by a lot of closing forms


This time & money business is a common excuse, however it is a fallacy. It's going cost you and your company 10 times as much struggling and fighting with this code now and the next time it needs to be updated vs. sitting down and thinking about the design and creating it the right way.
markmcgookin's Avatar
Moderator
 
Join Date: Dec 2006
Location: Northern Ireland / England
Posts: 546
#11: Dec 30 '08

re: StackOverflow followed by a lot of closing forms


Try telling that to my boss.... a small achievement now that he can wave infront of a customer makes him happy when the code needs a bloody redesign </rant>
RedSon's Avatar
Site Moderator
 
Join Date: Jan 2007
Location: America
Posts: 3,392
#12: Dec 30 '08

re: StackOverflow followed by a lot of closing forms


Yes I know... but it is our job as engineers to temper that short term gain for long term achievement. At each phase of development the cost to fix an error becomes exponentially greater. In Sven's case since he is at the final stage of development (maintenance) it is going to cost his company the most to fix the problems he is experiencing. However the company's ROI will be greater if they trash their previous program and design a better one that will hold up better to future development.
Member
 
Join Date: Oct 2008
Posts: 50
#13: Jan 7 '09

re: StackOverflow followed by a lot of closing forms


Would the singleton pattern be a possible solution?
This way I would only have one instance of the form that would be called.
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#14: Jan 7 '09

re: StackOverflow followed by a lot of closing forms


I have a rather simple solution that I request that you try.
If you display a form using ShowDialog then it won't be Disposed when you call Form.Close. i.e Try calling Form.Dispose() where you are calling Form.Close.
Reply