Of course that assumes that the Form is properly closed and is properly
disposing it's resources. After you close it, it's not a bad idea to
explicitly call Dispose() on it.
--
Chris Tacke, eMVP
Co-Founder and Advisory Board Member
www.OpenNETCF.org
---
Windows CE Product Manager
Applied Data Systems
www.applieddata.net
"bdoyle" <bdoy...@carenttech.com> wrote in message
news:14767AA3-7CA3-4600...@microsoft.com...
"Chris Tacke, eMVP" <cta...@spamfree-opennetcf.org> wrote in message
news:uAbfcVe4...@TK2MSFTNGP10.phx.gbl...
Keep in mind, that when you show a form via .ShowDialog, it is not disposed
automatically when closed (ByDesign). This is because modal dialogs are
intended to be reusable.
-Katie
This posting is provided "AS IS" with no warranties, and confers no rights.
***.Net Compact Framework Info***
Faq:
http://msdn.microsoft.com/mobility/prodtechinfo/devtools/netcf/FAQ/default.a
spx
QuickStarts: http://samples.gotdotnet.com/quickstart/CompactFramework/
Samples:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnnetcomp/h
tml/CompactfxTechArt.asp
--------------------
| Thread-Topic: Memory Leak in Form
| thread-index: AcPh4/WoPJJBgLV4TP68FOfoitSE4w==
| X-Tomcat-NG: microsoft.public.dotnet.framework.compactframework
| From: =?Utf-8?B?YmRveWxl?= <bdoy...@carenttech.com>
| Subject: Memory Leak in Form
| Date: Fri, 23 Jan 2004 11:06:09 -0800
| Lines: 2
| Message-ID: <14767AA3-7CA3-4600...@microsoft.com>
| MIME-Version: 1.0
| Content-Type: text/plain;
| charset="Utf-8"
| Content-Transfer-Encoding: 7bit
| X-Newsreader: Microsoft CDO for Windows 2000
| Content-Class: urn:content-classes:message
| Importance: normal
| Priority: normal
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
| Newsgroups: microsoft.public.dotnet.framework.compactframework
| Path: cpmsftngxa07.phx.gbl
| Xref: cpmsftngxa07.phx.gbl
microsoft.public.dotnet.framework.compactframework:43886
| NNTP-Posting-Host: tk2msftcmty1.phx.gbl 10.40.1.180
| X-Tomcat-NG: microsoft.public.dotnet.framework.compactframework
"Katie Schaeffer [MSFT]" <kat...@online.microsoft.com> wrote in message
news:KmDKLAh4...@cpmsftngxa07.phx.gbl...
When a form is displayed as a modal dialog box, clicking the close form
button (the button with an "X" at the top right of the form) causes the form
to be hidden and the DialogResult property to be set to DialogResult.Cancel.
Unlike modeless forms, the Close method is not called by the .NET Framework
when the user clicks the close form button of a dialog box or sets the value
of the DialogResult property. Instead the form is hidden and can be shown
again without creating a new instance of the dialog box. Because a form
displayed as a dialog box is not closed, you must call the Dispose method of
the form when the form is no longer needed by your application.
--
Tim Wilson
.Net Compact Framework MVP
"Martin Robins" <martin - robins @ ntlworld dot com> wrote in message
news:%236DnQrm...@tk2msftngp13.phx.gbl...
----- Katie Schaeffer [MSFT] wrote: -----
Ok - so I'll start with some of the differences between using .Show and
using .ShowDialog:
.ShowDialog - This gives you a modal dialog, which means that the
previously topmost form is disabled until this dialog is dismissed.
Dialogs are also meant to be re-usable, allowing you to call .ShowDialog
multiple times after the dialog has been closed (either via the user
clicking the 'ok' button on the top of your ppc, or programmitically by
calling Close() or setting the DialogResult property). This is typically
used when information is to be collected or displayed to the user, and the
user needs to provide or acknowledge that information before interacting
with the application further. Also, a call to .ShowDialog will not return
until the dialog shown is closed.
.Show - This simply makes a form visible. It doesn't disable the previous
form, and you need to do extra work to prevent the form from being disposed
upon closing (such as set CancelEventArgs.Cancel = True in the closing
event and hide the form). Code after the call to .Show will execute
immediately (without waiting for the form you just displayed to be closed).
As far as best practices for application flow, you can pretty much make any
combination of .Show and .ShowDialog work, but here are my thoughts about
what is easiest. Applications that have a very linear path (Form A -> Form
B -> Form C -> Form B -> Form D -> Form B -> Form A) would use .ShowDialog.
It is expected that only one form is being interacted with at a time, and
when you close a form you tend to display the previous form. Applications
that are navigated more freely (Form A -> Form B -> Form C -> Form A) may
have an easier time using .Show. Notice that in this case after Form C is
closed, you want to go back to the main form.
If you're running into memory problems - you can ensure that only forms
currently visible are kept around. When using .ShowDialog, you can add a
this.Dispose call to your Closed event. When using .Show, this is already
done for you. (If the form has been disposed, you'll have to create a new
instance before showing it again - myForm2 = new Form2(); myForm2.Show.
You'll know right away if you forgot to do this, since you'll get an
ObjectDisposedException). Of course, re-creating forms unnecessarily will
have performance implications, so I'd recommend re-using forms whenever
possible. If you're using graphics and bitmap objects - be sure to Dispose
those, as they can eat up quite a bit of memory.
As far as message pumps - the typical way to start an application is to use
Application.Run. This designates a main form and sets up a message pump.
When this main form is closed, that message pump will also shut down. A
call to .ShowDialog also sets up a message pump (that shuts down when the
dialog is closed). All the message pump does is loop continuously
processing events, until the respective form is closed. This means code
placed directly after the call to .ShowDialog, or Application.Run doesn't
get executed until the respective form is closed.
Ok - I think that answers most of your questions (and hopefully I don't
have too many typos). My advice is to start up a simple application with a
few forms, and play around with this stuff. Also, the links below are good
sources of information. Good Luck :-).
-Katie
This posting is provided "AS IS" with no warranties, and confers no rights.
***.Net Compact Framework Info***
Faq:
http://msdn.microsoft.com/mobility/prodtechinfo/devtools/netcf/FAQ/default.a
spx
QuickStarts: http://samples.gotdotnet.com/quickstart/CompactFramework/
Samples:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnnetcomp/h
tml/CompactfxTechArt.asp
--------------------
| Thread-Topic: Memory Leak in Form
| thread-index: AcPkO/U44/9rUyBmQRybdbk+vTYC+w==
| X-Tomcat-NG: microsoft.public.dotnet.framework.compactframework
| From: =?Utf-8?B?QkRPWUxF?= <bdoy...@carenttech.com>
| References: <14767AA3-7CA3-4600...@microsoft.com>
<KmDKLAh4...@cpmsftngxa07.phx.gbl>
| Subject: RE: Memory Leak in Form
| Date: Mon, 26 Jan 2004 10:41:06 -0800
| Lines: 60
| Message-ID: <149EF5E1-B997-424E...@microsoft.com>
| MIME-Version: 1.0
| Content-Type: text/plain;
| charset="Utf-8"
| Content-Transfer-Encoding: 7bit
| X-Newsreader: Microsoft CDO for Windows 2000
| Content-Class: urn:content-classes:message
| Importance: normal
| Priority: normal
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
| Newsgroups: microsoft.public.dotnet.framework.compactframework
| Path: cpmsftngxa07.phx.gbl
| Xref: cpmsftngxa07.phx.gbl
microsoft.public.dotnet.framework.compactframework:44050
try this code:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
public class TestForm3 : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox tb;
public TestForm3(string t)
{
InitializeComponent();
tb.Text = t;
}
private void InitializeComponent()
{
this.tb = new System.Windows.Forms.TextBox();
this.textBox1 = new System.Windows.Forms.TextBox();
this.tb.Text = "textBox1";
this.textBox1.Location = new System.Drawing.Point(104, 0);
this.textBox1.Text = "Top";
this.Controls.Add(this.textBox1);
this.Controls.Add(this.tb);
this.Load += new System.EventHandler(this.TestForm3_Load);
}
private void TestForm3_Load(object sender, System.EventArgs e)
{
this.MaximizeBox = false;
this.MinimizeBox = false;
this.ControlBox = false;
this.FormBorderStyle = FormBorderStyle.None;
this.WindowState = FormWindowState.Maximized;
this.Menu = null;
}
static void Main()
{
TestForm3 t = new TestForm3("1");
TestForm3 t2 = new TestForm3("2");
//first time
t.Show();
t.Refresh();
t2.Show();
t2.Refresh();
//second time location moved
t.Show();
t.Refresh();
t2.Show();
t2.Refresh();
t2.Hide();
System.Threading.Thread.Sleep(10000);
}
}
What gives?
Meanwhile, if you're looking for a workaround - you can try removing the
line 'this.FormBorderStyle = FormBorderStyle.None'. This makes your form
act as a popup form (also allowing it to be resized to not take up the
entire screen). The PocketPC OS has some known issues with it's handling
of WS_POPUP forms.
Hope this helps,
-Katie
This posting is provided "AS IS" with no warranties, and confers no rights.
***.Net Compact Framework Info***
Faq:
http://msdn.microsoft.com/mobility/prodtechinfo/devtools/netcf/FAQ/default.a
spx
QuickStarts: http://samples.gotdotnet.com/quickstart/CompactFramework/
Samples:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnnetcomp/h
tml/CompactfxTechArt.asp
--------------------
| Thread-Topic: Memory Leak in Form
| thread-index: AcPpzONTZJQ4VMJjS+alT8lTG+DfZg==
| X-Tomcat-NG: microsoft.public.dotnet.framework.compactframework
| From: =?Utf-8?B?YmRveWxl?= <bdoyle80...@carenttech.com>
| References: <14767AA3-7CA3-4600...@microsoft.com>
<KmDKLAh4...@cpmsftngxa07.phx.gbl>
<149EF5E1-B997-424E...@microsoft.com>
<FY3PKMI...@cpmsftngxa07.phx.gbl>
| Subject: RE: Memory Leak in Form
| Date: Mon, 2 Feb 2004 12:41:09 -0800
| Lines: 64
| Message-ID: <7DD402F1-EAF1-4AE3...@microsoft.com>
| MIME-Version: 1.0
| Content-Type: text/plain;
| charset="Utf-8"
| Content-Transfer-Encoding: 7bit
| X-Newsreader: Microsoft CDO for Windows 2000
| Content-Class: urn:content-classes:message
| Importance: normal
| Priority: normal
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
| Newsgroups: microsoft.public.dotnet.framework.compactframework
| Path: cpmsftngxa07.phx.gbl
| Xref: cpmsftngxa07.phx.gbl
microsoft.public.dotnet.framework.compactframework:44654
| NNTP-Posting-Host: tk2msftcmty1.phx.gbl 10.40.1.180
| X-Tomcat-NG: microsoft.public.dotnet.framework.compactframework
|
I have tonight been running some examples and cannot necessarily reproduce
what you are saying.
I have a CF application with 2 forms. The first form has a timer that, on
the tick event, creates a new instance of the second form and displays it
using ShowDialog(). The second form also has a timer that then sets
DialogResult to DialogResult.OK in order to close that form. When I run the
application, the memory is used up constantly as you (and the original
writer) describe. If I add a form.Dispose() after the form.ShowDialog(), the
memory usage does not increase, also as described.
If I replace the this.DialogResult = DialogResult.OK with this.Close(), and
again disable the form.Dispose() after the form.ShowDialog(), the memory
usage remains steady with no noticable increase in the memory being used.
According to your message, the results should be the same for both
scenarios.
Incidentally, I am running CF SP2 for these tests in case that makes a
difference.
Regards,
Martin.
"Katie Schaeffer [MSFT]" <kat...@online.microsoft.com> wrote in message
news:FY3PKMI...@cpmsftngxa07.phx.gbl...
> Hi,
>
> Ok - so I'll start with some of the differences between using .Show and
> using .ShowDialog:
>
> ShowDialog - This gives you a modal dialog, which means that the
> previously topmost form is disabled until this dialog is dismissed.
> Dialogs are also meant to be re-usable, allowing you to call .ShowDialog
> multiple times after the dialog has been closed (either via the user
> clicking the 'ok' button on the top of your ppc, or programmitically by
> calling Close() or setting the DialogResult property). This is typically
> used when information is to be collected or displayed to the user, and the
> user needs to provide or acknowledge that information before interacting
> with the application further. Also, a call to .ShowDialog will not return
> until the dialog shown is closed.
>
What you are saying makes sense. If you use .ShowDialog, the form will not
automatically be disposed upon closing (either via setting the DialogResult
or via Close()). If you re-use the same form instance and call .ShowDialog
multiple times, you shouldn't see the memory usage increase. If, however,
you create a new instance, then it's expected that the memory usage will
increase.
Am I missing something?
-Katie
This posting is provided "AS IS" with no warranties, and confers no rights.
***.Net Compact Framework Info***
Faq:
http://msdn.microsoft.com/mobility/prodtechinfo/devtools/netcf/FAQ/default.a
spx
QuickStarts: http://samples.gotdotnet.com/quickstart/CompactFramework/
Samples:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnnetcomp/h
tml/CompactfxTechArt.asp
--------------------
| From: "Martin Robins" <martin - robins @ ntlworld dot com>
| References: <14767AA3-7CA3-4600...@microsoft.com>
<KmDKLAh4...@cpmsftngxa07.phx.gbl>
<149EF5E1-B997-424E...@microsoft.com>
<FY3PKMI...@cpmsftngxa07.phx.gbl>
| Subject: Re: Memory Leak in Form
| Date: Thu, 5 Feb 2004 21:28:59 -0000
| Lines: 224
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| Message-ID: <e1ZfO8C7...@TK2MSFTNGP10.phx.gbl>
| Newsgroups: microsoft.public.dotnet.framework.compactframework
| NNTP-Posting-Host: cpc3-shep1-5-0-cust37.nott.cable.ntl.com 213.105.123.37
| Path:
cpmsftngxa07.phx.gbl!cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP10.
phx.gbl
| Xref: cpmsftngxa07.phx.gbl
microsoft.public.dotnet.framework.compactframework:45040
| X-Tomcat-NG: microsoft.public.dotnet.framework.compactframework
For the test I am creating a new instance of the form every time. Using
DialogResult causes the memory usage to increase dramaticcally, using
Close() causes no memory use increase at all.
I am not manually disposing the form at all during the tests.
Martin.
"Katie Schaeffer [MSFT]" <kat...@online.microsoft.com> wrote in message
news:a9m28JP7...@cpmsftngxa07.phx.gbl...
"Martin Robins" <martin - robins @ ntlworld dot com> wrote in message
news:eZmC6yX7...@TK2MSFTNGP10.phx.gbl...
"Alex Feinman [MVP]" <publi...@alexfeinman.com> wrote in message
news:%23dHAVie...@TK2MSFTNGP10.phx.gbl...
So for both your tests you are using .ShowDialog, creating a new form
instances, and never calling .Dispose()? You should be seeing your memory
usage increase under both cases then.
So I just checked our SP2 code for setting DialogResult vs. calling Close
and it is exactly the same. Are you sure there isn't some other place
where there is different code getting run under these cases?
Feel free to post all the code or send me a repro and I'll look at it.
-Katie
This posting is provided "AS IS" with no warranties, and confers no rights.
***.Net Compact Framework Info***
Faq:
http://msdn.microsoft.com/mobility/prodtechinfo/devtools/netcf/FAQ/default.a
spx
QuickStarts: http://samples.gotdotnet.com/quickstart/CompactFramework/
Samples:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnnetcomp/h
tml/CompactfxTechArt.asp
--------------------
| From: "Martin Robins" <martin - robins @ ntlworld dot com>
| References: <14767AA3-7CA3-4600...@microsoft.com>
<KmDKLAh4...@cpmsftngxa07.phx.gbl>
<149EF5E1-B997-424E...@microsoft.com>
<FY3PKMI...@cpmsftngxa07.phx.gbl>
<e1ZfO8C7...@TK2MSFTNGP10.phx.gbl>
<a9m28JP7...@cpmsftngxa07.phx.gbl>
| Subject: Re: Memory Leak in Form
| Date: Sat, 7 Feb 2004 13:17:30 -0000
| Lines: 357
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| Message-ID: <eZmC6yX7...@TK2MSFTNGP10.phx.gbl>
| Newsgroups: microsoft.public.dotnet.framework.compactframework
| NNTP-Posting-Host: cpc3-shep1-5-0-cust37.nott.cable.ntl.com 213.105.123.37
| Path:
cpmsftngxa07.phx.gbl!cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP10.
phx.gbl
| Xref: cpmsftngxa07.phx.gbl
microsoft.public.dotnet.framework.compactframework:45188
| X-Tomcat-NG: microsoft.public.dotnet.framework.compactframework
I've looked carefully into the implementation details, and from what I see,
I was wrong and there should be no difference between calling Close and
setting DialogResult provided the form is displayed via ShowDialog. My
information was outdated - from the beta timeframe. The form is not
destroyed and it is your responsibility to call Dispose when you are done or
make sure you have no outstanding references to the form to allow the GC to
collect it. This is different from pre-SP2 where you must have called
Dispose, because the GC was prevented from collecting an instance of a form
via internal reference that never cleared.
Can you confirm that you are using SP2?
"Martin Robins" <martin - robins @ ntlworld dot com> wrote in message
news:OkqMeuj7...@TK2MSFTNGP12.phx.gbl...