By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,887 Members | 1,150 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,887 IT Pros & Developers. It's quick & easy.

Switching from 1 loaded winform to another hangs the loading of 2nd form

P: n/a
Hello All,

I am working on a .Net desktop application and I am having problem
displaying a form. I am using C# and version 1.1 of the framework. here
is how the code looks likes and I will appreciate if someone can tell
me how to resolve this problem.

Main ( Args )
{
Form1 f1 = new Form1();
Form2 f2 = new Form2();
Form1 f3 = new Form3();

//based on some passed paramter I decide which form to load
LoadForm(arg);
}

LoadForm( Args)
{
if Args[0] = "1"
f1.ProcessCommand(args); // I pass the paramters and the form
// loads itself and is activated.
// Same goes for all the other forms also.
if Args[0] = "2"
f2.ProcessCommand(args);
if Args[0] = "3"
f3.ProcessCommand(args);
}
Now this application is supposed to be running on the background even
if I close the form. This part is working OK.

When I am done using a form I call the Close() but I dont release the
resources used by the form as the user might load the same form again.

When I load any form, lets say f1 .. the application works with out any
problem. When I close the f1 using Close() and try to load lets say f2
the form f2 loads up but after the activate event only the borders
appears and the client area is all white as if the form is unable to
draw itself.
Please note I am using delegates in the the forms to show the form and
process the various events.

Also note all forms work using the design.i.e using delegates to invoke
methods.
When I run the application and load either of the three forms
everything works fine but when I try to load another form after using
one form this problem arises.

your help will be greatly appreciated.

Cheers...

Nad

Nov 17 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Hi Nad,

[STAThread]
static void Main()
{
Application.Run(new Form1()); <- the Form1-object becomes your "main form"
MessageBox.ShowMessage("if you see me Form1 was closed and the program is about to come to an end");
}
If you close the main form you "close" your program.

Now I'm not sure if you are even using Application.Run() and from your code I can't see where/when you would open/display a second form. In any case, once u have called the Close() method of a form that was it, you can't "re"-show that same form-object (you can create a new one and display that one). The data of the "closed" form still "floats" around for a while until the garbage collector feels like having a go at it, so your program (referencing different form objects from one another) might still tend to work a bit, but not very safely (as you have encountered).

If you want to turn your form on/off use the visible property.

I've also encountered that after closing a form the object lives a lot longer than I would have thought. I have gone over to using the using clause on a regular basis, thus preventing the after life of a closed but still not disposed form.

using (Form1 myFrmObject = new Form1()) {
myFormObject.ShowDialog();
} // here the darn object is history.

So quintessence is:
- close the main form -> good bye program
- close a form -> form is still there, but you cannot call Show/ShowDialog to show it again.
- using can help prevent problems with form objects "bumming" around in memory

hope this helps
<na********@yahoo.com> schrieb im Newsbeitrag news:11**********************@o13g2000cwo.googlegr oups.com...
Hello All,

I am working on a .Net desktop application and I am having problem
displaying a form. I am using C# and version 1.1 of the framework. here
is how the code looks likes and I will appreciate if someone can tell
me how to resolve this problem.

Main ( Args )
{
Form1 f1 = new Form1();
Form2 f2 = new Form2();
Form1 f3 = new Form3();

//based on some passed paramter I decide which form to load
LoadForm(arg);
}

LoadForm( Args)
{
if Args[0] = "1"
f1.ProcessCommand(args); // I pass the paramters and the form
// loads itself and is activated.
// Same goes for all the other forms also.
if Args[0] = "2"
f2.ProcessCommand(args);
if Args[0] = "3"
f3.ProcessCommand(args);
}


Now this application is supposed to be running on the background even
if I close the form. This part is working OK.

When I am done using a form I call the Close() but I dont release the
resources used by the form as the user might load the same form again.

When I load any form, lets say f1 .. the application works with out any
problem. When I close the f1 using Close() and try to load lets say f2
the form f2 loads up but after the activate event only the borders
appears and the client area is all white as if the form is unable to
draw itself.
Please note I am using delegates in the the forms to show the form and
process the various events.

Also note all forms work using the design.i.e using delegates to invoke
methods.
When I run the application and load either of the three forms
everything works fine but when I try to load another form after using
one form this problem arises.

your help will be greatly appreciated.

Cheers...

Nad

Nov 17 '05 #2

P: n/a
Hi,

Are you using ShowDialog ?

Without seeing the code you use to load/show the form is difficult to see
the error.

Also , IIRC when you call Close the form is disposed:
From MSDN's Form.Close():
************************
Remarks
When a form is closed, all resources created within the object are closed
and the form is disposed. You can prevent the closing of a form at run time
by handling the Closing event and setting the Cancel property of the
CancelEventArgs passed as a parameter to your event-handling method. If the
form you are closing is the startup form of your application, your
application ends.

Note When the Close method is called on a Form displayed as a modeless
window, you cannot call the Show method to make the form visible, because
the form's resources have already been released. To hide a
form and then make it visible, use the Control.Hide method.

************************

As you can see you have to use Hide() instead
Why are you using such a complex load schema?
Why dont simply call formXXX.Show in the LoadForm method?

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation


<na********@yahoo.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
Hello All,

I am working on a .Net desktop application and I am having problem
displaying a form. I am using C# and version 1.1 of the framework. here
is how the code looks likes and I will appreciate if someone can tell
me how to resolve this problem.

Main ( Args )
{
Form1 f1 = new Form1();
Form2 f2 = new Form2();
Form1 f3 = new Form3();

//based on some passed paramter I decide which form to load
LoadForm(arg);
}

LoadForm( Args)
{
if Args[0] = "1"
f1.ProcessCommand(args); // I pass the paramters and the form
// loads itself and is activated.
// Same goes for all the other forms also.
if Args[0] = "2"
f2.ProcessCommand(args);
if Args[0] = "3"
f3.ProcessCommand(args);
}
Now this application is supposed to be running on the background even
if I close the form. This part is working OK.

When I am done using a form I call the Close() but I dont release the
resources used by the form as the user might load the same form again.

When I load any form, lets say f1 .. the application works with out any
problem. When I close the f1 using Close() and try to load lets say f2
the form f2 loads up but after the activate event only the borders
appears and the client area is all white as if the form is unable to
draw itself.
Please note I am using delegates in the the forms to show the form and
process the various events.

Also note all forms work using the design.i.e using delegates to invoke
methods.
When I run the application and load either of the three forms
everything works fine but when I try to load another form after using
one form this problem arises.

your help will be greatly appreciated.

Cheers...

Nad

Nov 17 '05 #3

P: n/a
Hello Robert,

Thanks for the reply.
OK let me tell you some more detail. This application stays in the
background even when no form is on the screen.
Yes I am using Application.Run( f1 );
This how it all works.

This application works as an Add-In. User can add it to the Main
Application's menu.
1. User loads the application and passes the name of the form using
Main ( Args).
2. User uses the form and closes it. ( I just hide the form.)
3. The application is running on the background.
4.Users selectes another option from the menu and this option tells my
application which form f1, f2 or f3 to load.
5. Again the application goes to step 1 through 4.

I think its a problem with switching from 1 UI thread to another. and I
have not seen any documantion which tells us that how many UI threads
are created if you have a situation like mine.

The application does not throw any exceptions it just fails to redraw
the client area of the new form.

Nov 17 '05 #4

P: n/a
No I am using Application.Run().

And which ever form user laods first I set its cancel to true in the
closing event so that it stays on the background.

Now if I try to load another form, this new form fails to darw itself.

I still feel there is a problem in switching from one UI thead to
another if there is something like 2nd UI thread.

Any links that might help me in understanding such cases will also be
helpful.

Thanks for the reply.

Nov 17 '05 #5

P: n/a
Hi,

IIRC you are suppost to use Application.Run only once, this create a message
pump and you should have only one per app.

There is no thing like second UI thread. win app are STA (single-threaded
apartment)

just do as I said, call Hide instead of Close.

Create the main window hide, it does decide which windows to show and when
this form close then the app ends.

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
<na********@yahoo.com> wrote in message
news:11*********************@g44g2000cwa.googlegro ups.com...
No I am using Application.Run().

And which ever form user laods first I set its cancel to true in the
closing event so that it stays on the background.

Now if I try to load another form, this new form fails to darw itself.

I still feel there is a problem in switching from one UI thead to
another if there is something like 2nd UI thread.

Any links that might help me in understanding such cases will also be
helpful.

Thanks for the reply.

Nov 17 '05 #6

P: n/a
Hello all..

Thanks for the help.

The problem seemed to be with the remoting. But thanks anyway for the
tips.

Nad

Nov 17 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.