|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Generated Program.cs uses special way of disposing native resourceTo 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 On Tue, 17 Apr 2007 04:54:00 -0700, martin wrote:
Show quoteHide quote > [STAThread] I'm not sure what you mean by "I stopped using Application.Run()". Anyway,> 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 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 Because the documentation of Application.Run states that the form's Dispose> into some kind of "using" statement? Is there some kind of special case going > on here? method will be called before Run() returns. 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. >
Other interesting topics
Databind a nullable date to a masked textbox
Graphics dpi not correct This code compiles/runs but breaks designer... Adding an event handler to a dynamically populated control Datagridview help Login window vs Main window Newbie question: how to set focus to a usercontrol How to read Default Values from App.Config for User Settings? datagridview multiselect without keyboard ! Forms and labels |
|||||||||||||||||||||||