窗体单例模式的实现:

 

public static ChatForm newForm = null;

public static ChatForm GetInstance()
        {
            
if (newForm == null || newForm.IsDisposed == true)//newForm.IsDisposed == true必需,否则会出现“访问已释放资源”的异常
            {
                newForm 
= new ChatForm();
            }
            
else
            {
                newForm.Activate();
            }
            
return newForm;
        }

//调用:
ChatForm newForm = ChatForm.GetInstance();
newForm.Ipcon 
= Ip;
newForm.Show();

 

 

单进程的实现:

 

[STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(
false);
            
// get the name of our process
            string proc = Process.GetCurrentProcess().ProcessName;
            
// get the list of all processes by that name
            Process[] processes = Process.GetProcessesByName(proc);
            
// if there is more than one process
            if (processes.Length > 1)
            {
                MessageBox.Show(
"程序已经运行!");
                
return;
            }
            
else
                Application.Run(
new MainForm());
        }

相关文章:

  • 2021-05-30
  • 2021-09-18
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-09-29
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-06-12
  • 2021-10-24
  • 2021-12-27
  • 2021-12-30
  • 2022-01-25
  • 2022-12-23
相关资源
相似解决方案