472,978 Members | 2,337 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,978 software developers and data experts.

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

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
6 4054
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: David N | last post by:
Hi All, The OnLoad() event is invoked automatically when a form is being loaded. Do we have another event that is invoked automatically after a form has completed loading? Thanks.
2
by: Tom | last post by:
Hi all In winform application I am trying to start 2 forms I am trying to show the 2nd winform on a separate thread. using Thread mainapp = new Thread(new ThreadStart(loadmainscm)); ...
7
by: Tim T | last post by:
Hi, I have the need to use dynamically loaded user controls in a webform page. I have the controls loading dynamically, and that part works fine. this is the code used in a webform to dynamically...
4
by: Jeremy Holt | last post by:
Hi, In a windows.forms application I would BeginInvoke a delegate on the UI thread to collect data from a database. When the call returns to the AsyncCallback, if the Control.InvokeRequired =...
3
by: Simon Verona | last post by:
I have a parent form which contains a toolbar. The toolbar controls the loading and switching to of MDI child forms. The code for the toolbar click event and one of the subroutines that loads...
14
by: Brett Romero | last post by:
I'm using a DataGrid and have assigned this.DataSourceChanged += new EventHandler( DataGrid_DataSourceChanged ); This works fine but there is one case where it doesn't. How can I check if...
2
by: Chen Zhuo | last post by:
Hi all experts, We are having a problem with the exact time when a C# dll gets loaded in managed C++. The scenario is like: In managed.cpp: #using MyCSharp.dll
4
by: moondaddy | last post by:
I have a .net 1.1 winforms app that calls an aspx page which I need to debug. I also need to start the debugging process from the winform because the winform first calls a web service which passed...
9
by: somacore | last post by:
I have a winform with a combobox. This combobox's text is set to a value from the database (in this case, a number). In my code I take the number and convert it to the proper text, returning the...
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.