破坏形式 [英] Destroying the form

查看:263
本文介绍了破坏形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我该怎么做?



销毁 p>

一个例子(Form1是某种形式):

  static void Main )
{
Test();
//这里我们仍然有一个活着
// GC.Collect()不会帮助

Form1 B = new Form1();
Application.Run(B);
//问题在这里:B和A碰撞,由于假设A已经死了
}

public static Test()
{
Form1 A = new Form1();
//用A做一些事情,但不能显示表单
//我确定A在Test()后会消失()
}

这个问题是否可以在表单上使用某些组件?定时器,它是在构造函数中创建的?或者它是一种正常的方式让A存在很久?

更新



碰撞 - 意味着他们正在使用一些仅限于单个用户的东西。我怎么能推动从内存中删除A的过程?请注意,不要急于(感谢-2),我在工作,所以不能更新得更快。我试图尽可能清楚。



只要有可能,请避免使用陈述 GC.Collect总是很糟糕等。我知道。正如您所看到的,我需要在软件运行之前对表单进行一些操作,因此不会出现性能问题或任何其他类型的问题。这可能不是最好的情况,但是请你告诉我更好的一个。使用A做某事是必要的测试,是的。



问题是:在对A进行测试之后,我希望它消失。当然,我可以以某种方式修改A,它不会有定时器,组件等。但是已经有几十种形式。所有都必须经过测试。我只是不知道在Test()之后会存在什么A.



更新2



I真的想知道为什么问题有-3。我今天学到的是当你创建一个表单时,它会一直存在,直到应用程序结束,现在已知方法杀死它。在构造函数中创建的定时器将继续运行,组件将尝试访问它们不应该的东西等等。只要您关心创建第二个工作实例(看起来),就会发生很多事情。



更新3



让我们做一个简单的测试:

 静态类程序
{
public static bool Test {get;组; }
static void Main()
{
DoTest();
Test = true;
Application.Run(new Form1());


static void DoTest()
{
var A = new Form1();
var B = new Form1();
var C = new Form1();
//A.Dispose();
//B.Dispose();
//C.Dispose();


$ b $ public partial class Form1:Form
{
public Form1()
{
InitializeComponent();
timer1.Start();


private void timer1_Tick(object sender,EventArgs e)
{
if(Program.Test)
{
timer1.Stop ();
MessageBox.Show(123);



code $
$ b $ p $它会显示4个消息框(从主窗口和 undead A,B,C)。在今天之前,我百分之百地确信,无论我把表格放在什么位置都会被删除。每当我创建一个局部变量 - 它将是本地的。但它似乎不是。这是一个问题 - 显而易见的行为。



它看起来像Thorsten Dittmar解决方案( Dispose()应该做个窍门。我应该看看自己的组件(手动关闭定时器等)。

处置()
。即使这并不意味着所有的内存都会立即释放,但如果窗体编程正确,那么所有由窗体(定时器,文件等)打开的资源都应该关闭。



当然,下次GC运行时窗体也会自动处理,但可以通过调用 Dispose()来手动消失你自己。然后,当GC感觉像是在运行并处理内存时,GC会运行,但是窗口占用的其他资源应该被释放。



最简单的方法是:

  public static Test(){
using(Form1 A = new Form1()){
//使用A做一些事情,但不显示表单
//我确定A会在Test()之后消失()
}
}


How can I do that?

Under destroying I mean remove it from the memory.

An example (Form1 is some form):

static void Main()
{
    Test();
    // here we still have A alive
    // GC.Collect()  doesn't helps

    Form1 B = new Form1();
    Application.Run(B);
    // problem is here: B and A "collides", due to assumption what A is already dead
}

public static Test()
{
    Form1 A = new Form1();
    // do something with A, but not displaying form
    // I was sure what A will disappears after Test()
}

Could the problem be certain components used on form? Timers, which are created in constructor? Or is it a normal way for A to exists that long?

Update

Collides - means they are using something what is exclusive to a single user only. How can I push the process of removing A from memory?

Please, do not be rushy (thanks for -2), I am at work, so can't update fast. I am trying to be as clear as I can.

Whenever you can, please avoid using cliches GC.Collect is always bad, etc. I know that. As you can see all I need a sort of manipulations with the form before software runs, so no performance or any other sort of issue is expected. It may be not the best case, but please then, tell me the better one. Doing something with A is a necessary test, yes.

The problem is: after doing test with A, I want it to disappear. Of course I can modify A in a way what it will not have timers, components, etc. But there are dozens of forms already. And all has to be tested. And I simply didn't knew that what A will exists after Test().

Update 2

I really want to know why question has -3. What I learned today is when you create a form, it will exists until the end of application and there is now known way to kill it. Timers, created in constructor will keep running, components will try to access something they shouldn't, etc. Whole lots of things will changes as soon as you care to create second instance of working (as it seems) form.

Update 3

Lets make a simple test:

static class Program
{
    public static bool Test { get; set; }
    static void Main()
    {
        DoTest();
        Test = true;
        Application.Run(new Form1());
    }

    static void DoTest()
    {
        var A = new Form1();
        var B = new Form1();
        var C = new Form1();
        //A.Dispose();
        //B.Dispose();
        //C.Dispose();
    }
}

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        timer1.Start();
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        if (Program.Test)
        {
            timer1.Stop();
            MessageBox.Show("123");
        }
    }
}

It will show 4 message boxes (from main window and from undead A, B, C). Before today I was 100% sure, what whatever I put on form will be deleted together with form. And whenever I create a local variable - it is going to be local. But it seems it is not. And this is a problem - un-obvious behavior.

It looks like Thorsten Dittmar solution with Dispose() should do a trick. I should look myself into my components (shutting their timers manually, etc).

解决方案

You should call Dispose(). Even if that doesn't mean all memory gets freed immediately, all resources opened by the form (timers, files, etc.) should be closed then, if the form is programmed properly.

Of course, the form will also be disposed of automatically when the GC runs the next time, but you can make it "disappear" manually by calling Dispose() yourself. Then the GC will run when it feels like running and take care of the memory, but other resources occupied by the window should be released.

The easiest way to do this is:

public static Test() {
    using (Form1 A = new Form1()) {
        // do something with A, but not displaying form
        // I was sure what A will disappears after Test() 
    }
}

这篇关于破坏形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆