从从不同形式调用的静态类中多次打开表单 [英] Opening a Form multiple times from a static class which is called from different forms

查看:128
本文介绍了从从不同形式调用的静态类中多次打开表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我打开一个名为FormX的表单,对其公共(非静态)变量Var进行一些处理。 Var从调用表单中设置并处理并返回以进行输出。我在执行过程中多次打开它。所有表单都同时运行。它工作正常,因为每个FormX是使用来自其父窗体的对象的独立实例打开。但是我在我的项目中有一个公共的静态类Abc可以访问所有形式,如果我添加一个代码:

If I open a Form named FormX that does some processing on its public (not static) variable "Var". "Var" is set from a calling form and processed and returned for output. I open it from multiple forms multiple times during execution. All forms are running simultaneously. It works fine because each FormX is opened using a seperate instance of an object from its parent form. But I have a public static class "Abc" in my project which is accessible to all forms and if I add a code:

public static class Abc
{
  public static string Open_form(string Var)
  {
    FormX Obj = new FormX();
    Obj.Var = Var;
    Obj.Show();
    Obj.Process_Var();
    Var = Obj.Var;
    Obj.Close();
    return Var;
  }
}

)like:

Form1_Load()
{
  //Some operations
  MessageBox.Show(Abc.Open_form("ABC"));

  //Some operations
  MessageBox.Show(Abc.Open_form("XYZ"));
}

Form2_Load()
{
  //Some operations
  MessageBox.Show(Abc.Open_form("123"));

  //Some operations
  MessageBox.Show(Abc.Open_form("456"));
}

Form1和Form2从主窗体打开为:

Form1 and Form2 are opened from a main Form as:

Form1 obj1 = new Form1();
obj1.Show();

Form2 obj2 = new Form2();
obj2.Show();

那么它会导致任何问题吗?如果两个形式同时调用静态调用,在处理变量Var时会有任何问题吗?还有一个问题,我应该在之后调用 FormX.Dispose(); FormX.Close(); 为什么? FormX.Dispose()

Then will it cause any problem? Will there be any problem in the processing of variable "Var" if two forms call the static call at the same time? One more question, should I call FormX.Dispose(); after FormX.Close(); Why and why not? Any benefit of FormX.Dispose() ?

推荐答案

c $ c> FormX.Var 是一个 static 属性或字段,然后是,当它被多个线程或对象使用时可能会有问题与此同时。如果 FormX.Var 不是 static ,那么没有,应该没有问题。

If FormX.Var is a static property or field, then yes, there may be issues when it is used by multiple threads or objects at the same time. If FormX.Var is not static, then no, there should be no issues.

经验法则:始终在实现 IDisposable 的任何对象上调用 Dispose 。 GC有一个方法为你调用这个,但它总是最好的做法,自己调用它。为了方便起见,使用C#的使用关键字。下面的语法自动调用 Dispose

Rule of thumb: Always call Dispose on any object that implements IDisposable. The GC has a means of calling this for you, but it's always the best practice to call it yourself. To facilitate this, use C#'s using keyword. The syntax below automatically calls Dispose.

using (FormX Obj = new FormX())
{
    ...
}

您很少需要直接调用关闭。看看你的代码,你似乎想显示FormX作为一个模式表单。也就是说,您希望用户使用表单,也可以在完成后点击确定按钮。然后,您正在读取 Var 属性的值,以获取用户输入的信息。在这种情况下,您需要调用 ShowDialog ,而不是显示 ShowDialog 调用只会在表单关闭时返回。调用后关闭 ShowDialog 没有效果,因为表单已关闭。

You rarely need to call Close directly. Looking at your code, it appears that you want to display FormX as a modal form. That is, you want the user to work with the Form and maybe hit an OK button when they're done. Then you're reading off the value of the Var property to get the information the user has entered. In this scenario, you need to call ShowDialog instead of Show. The ShowDialog call will only return when the form is closed. Calling Close after ShowDialog has no effect because the form is already closed.

所有的,我相信你的代码应该看起来像这样:

All told, I believe your code should look like this:

    public static string Open_form(string Var)
    {
        using (FormX Obj = new FormX())
        {
            Obj.Var = Var;
            Obj.ShowDialog();
            Obj.Process_Var();
            return Obj.Var;
        }
    }

这篇关于从从不同形式调用的静态类中多次打开表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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