c#单例课程运行良好确认 [英] c# singleton class working well confirmation

查看:87
本文介绍了c#单例课程运行良好确认的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要确认一个单例模式。

I need a confirmation about a singleton pattern.

我有一个单例类,并将其用作dll。我编写了一个对此dll进行引用的程序,并将其称为单例类。
我写了第二个程序,我也做同样的事情。

I've a singleton class and I'm using it as dll. I write a program with a reference to this dll and I call my singleton class. I write a second program and I do the same.

我是对的,如果我告诉你,即使我有两个程序,它们也会调用我的singleton类的单个实例?

Am I right if I tell you that even if I have two program they call both a single instance of my singleton class?

我尝试增加一个静态变量instancenumber,每次通过构造函数时,instancenumber都会增加,并且两个程序都告诉我instancenumber为1。所以应该没问题,但是我需要您的建议可以。

I tried to increment an static variable instancenumber that increment each time I pass the constructor and both programs tell me that the instancenumber is 1. So it should be ok but I need your advice to be sure.

谢谢。最好的问候

单例类:

namespace SingletonClassLib
{
    public class SingletonClass
    {
        public static int InstanceNumber = 0;
        private static string myName;
        #region //Singleton initialisation
        private SingletonClass() { createInstance(); }
        private void createInstance()
        {
            InstanceNumber++;
            myName = myName + InstanceNumber.ToString();
        }
        public static SingletonClass _I { get { return NTCSession._i; } }
        private class NTCSession
        {
            static NTCSession() { }
            internal static readonly SingletonClass _i = new   SingletonClass();
        }
        private static List<WeakReference> instances = new List<WeakReference>();
        #endregion //Singleton initialisation
        public static string askname { get { return myName; } }
    }
    }

程序1:

using System.Windows.Forms;
using SingletonClassLib;

namespace Singletontest
{

public partial class Form1 : Form
{
    SingletonClass myclass = SingletonClass._I;
    public Form1()
    {
        InitializeComponent();
        string Name = SingletonClass.askname;
        MessageBox.Show("Program first: "+Name);
    }
}
}

程序2:

using System.Windows.Forms;
using SingletonClassLib;

namespace Singletontest
{

public partial class Form1 : Form
{
    SingletonClass myclass = SingletonClass._I;
    public Form1()
    {
        InitializeComponent();
        string Name = SingletonClass.askname;
        MessageBox.Show("Program second: "+Name);
    }
}
}

推荐答案


我是对的,即使我有两个程序,它们也会同时调用我的单例课程的单个实例?

Am I right if I tell you that even if I have two program they call both a single instance of my singleton class?

否。那不是它的工作原理。您的每个程序都会在其内存空间中加载dll的副本,因此每个程序都有其自己的类实例。您可以启动您的第一个程序5次,而您的第二个程序3次,那么您的班级将有8个实例。

No. That is not how it works. Each of your programs loads a copy of the dll in their memory space and so each of your programs has it's own instance of the class. You can start your first program 5 times and your second program 3 times and there will be 8 instances of your class.

它们不会共享。

如果需要证明,只需进行证明,以便可以 write 到实例,例如 set 名称。您将看到,无论您在一个程序中执行什么操作,其他程序都不会看到这些更改。

If you need proof, just make it so that you can write to the instance, for example set the name. You will see that no matter what you do in one program, the others won't be seeing those changes.

在应用程序之间共享数据的方式不同,共享dll中的类不是其中之一,因为每个程序都加载它自己的dll副本。

There are different ways to share data between applications, a class in a shared dll is not one of them, because each program loads it's own copy of the dll.

这篇关于c#单例课程运行良好确认的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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