Home All Groups Group Topic Archive Search About

Generated Program.cs uses special way of disposing native resource

Author
17 Apr 2007 11:54 AM
martin

Hi,

To properly clean up native resources, one should always use "using (Blah
blah = new Blah())" for types "Blah" that implement IDisposable. It seems to
me that the code generated by Visual Studio in Program.cs doesn't live by
this rule? A form is always IDisposable, so who calls the .Dispose() method
on the form?

        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new MyForm());
        }


And what about, if I change the code into this (below):

        [STAThread]
        static void Main()
        {
            MyForm myForm = new MyForm();
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(myForm);
            myForm.DoSomethingThatUsesTheFormObjectExtensivly();
        }

The reason why I'm asking about this is because I'm having trouble with
system tray icons that are left after my application exits. Normally this
works fine, but when I stopped using Application.Run() things got hairy. I'm
not sure the normal Application.Run() because I'm trying to use
Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run()
instead, in order to get a clean implementation of "single instance
application".

My question is, why isn't the Form1() instance in the generated code wrapped
into some kind of "using" statement? Is there some kind of special case going
on here?


Regards,
Martin
Author
17 Apr 2007 2:12 PM
Mehdi
On Tue, 17 Apr 2007 04:54:00 -0700, martin wrote:

Show quoteHide quote
>         [STAThread]
>         static void Main()
>         {
>             MyForm myForm = new MyForm();
>             Application.EnableVisualStyles();
>             Application.SetCompatibleTextRenderingDefault(false);
>             Application.Run(myForm);
>             myForm.DoSomethingThatUsesTheFormObjectExtensivly();
>         }
>
> The reason why I'm asking about this is because I'm having trouble with
> system tray icons that are left after my application exits. Normally this
> works fine, but when I stopped using Application.Run() things got hairy. I'm
> not sure the normal Application.Run() because I'm trying to use
> Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run()
> instead, in order to get a clean implementation of "single instance
> application".

I'm not sure what you mean by "I stopped using Application.Run()". Anyway,
i had a similar problem a while ago with system tray icons sticking in the
system tray after the application exited and I think that i solved the
problem by disposing the tray icon object manually before the app exited.

> My question is, why isn't the Form1() instance in the generated code wrapped
> into some kind of "using" statement? Is there some kind of special case going
> on here?

Because the documentation of Application.Run states that the form's Dispose
method will be called before Run() returns.
Are all your drivers up to date? click for free checkup

Author
18 Apr 2007 8:36 AM
martin
Thanks Mehdi.

What I meant by "not using Application.Run()" is that instead of calling
this method I call
theMicrosoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run().
However, I'm not sure whether
Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run()
will actually call Application.Run() or not.

As you say the documentation for Application.Run() says that the form will
be disposed. When the form is disposed the notify icon will also be disposed
automatically. When I switched from using Application.Run() to
Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run()
it seems like the form (and thus also the notify icon) was no longer being
disposed correctly. I find this to be quite confusing, I wish they didn't
call dispose from application.run() but instead used the "using-statement"
because having a call to dispose inside application.run() is quite subtle.

Anyway, again thanks for replying!



Regards,
Martin



Show quoteHide quote
"Mehdi" wrote:
> On Tue, 17 Apr 2007 04:54:00 -0700, martin wrote:
>
> >         [STAThread]
> >         static void Main()
> >         {
> >             MyForm myForm = new MyForm();
> >             Application.EnableVisualStyles();
> >             Application.SetCompatibleTextRenderingDefault(false);
> >             Application.Run(myForm);
> >             myForm.DoSomethingThatUsesTheFormObjectExtensivly();
> >         }
> >
> > The reason why I'm asking about this is because I'm having trouble with
> > system tray icons that are left after my application exits. Normally this
> > works fine, but when I stopped using Application.Run() things got hairy. I'm
> > not sure the normal Application.Run() because I'm trying to use
> > Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run()
> > instead, in order to get a clean implementation of "single instance
> > application".
>
> I'm not sure what you mean by "I stopped using Application.Run()". Anyway,
> i had a similar problem a while ago with system tray icons sticking in the
> system tray after the application exited and I think that i solved the
> problem by disposing the tray icon object manually before the app exited.
>
> > My question is, why isn't the Form1() instance in the generated code wrapped
> > into some kind of "using" statement? Is there some kind of special case going
> > on here?
>
> Because the documentation of Application.Run states that the form's Dispose
> method will be called before Run() returns.
>

Bookmark and Share