开发者

How do I "Fire and forget" a WinForms Form?

开发者 https://www.devze.com 2022-12-23 23:50 出处:网络
What\'s a good technique to create a WinForms Form instance, display it (non-modally) but not have to keep a reference around to it?Normally, as soon as the variable goes out of scope, the form is clo

What's a good technique to create a WinForms Form instance, display it (non-modally) but not have to keep a reference around to it? Normally, as soon as the variable goes out of scope, the form is closed:

var form = new SuperDuperForm();
form.Show();
// Form has probably been closed instantly

I don't want to have to keep track of instances of the form, I want it so that when the user closes the form, it is disposed. One idea I've had that I'm going to implement is a kind of controller that I use to open and display forms, that will keep track of them and monitor when they are closed via callbacks.

I'm j开发者_如何学Cust wondering if there are any neat tricks to get away without that. Any ideas?


You don't have to do this at all - Winforms does it for you. When you show a form, it gets added to the Application.OpenForms, property, and when you close the form, it is automatically removed.

A Windows Form will never be eligible for garbage collection while it is open, and will also automatically be disposed when it is closed. Quoting from the Form.Close documentation:

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 handler. If the form you are closing is the startup form of your application, your application ends.

The two conditions when a form is not disposed on Close is when (1) it is part of a multiple-document interface (MDI) application, and the form is not visible; and (2) you have displayed the form using ShowDialog. In these cases, you will need to call Dispose manually to mark all of the form's controls for garbage collection.

In other words, for the general case when you "fire and forget", resource management is automatic.


I have no problems opening forms without keeping references to them. However, if you create the the form within a separate thread, it will vanish when the thread exits (which it will do immediately, since Show returns as soon as the form is displayed).


I think that you can instance a form without keeping a reference. One technique I have used is keeping track through a Dictionary myForms<sting, Form> collection. This meant that I could still subscribe to a .FormClosing event and get rid of the form in my collection.

Of course, if you want to do it the right way, you would use Application.OpenForms - see Aaronaught's answer. I only just found out about that property after reading his answer.


What about a simple "using" clause? Something like:

using (Form vForm = new Form()} { // your code goes here )

The instance is dismissed once you go out of scope of the "using". There is also the cheat of a simple call such as (new Form()).Show(); A quick test shows me a window that doesn't go away when the calling method closes.

0

精彩评论

暂无评论...
验证码 换一张
取 消