winform 单例模式 单线程例子

WinForm 单例

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();

单线程

         static void Main()
        {
            //DevExpress.UserSkins.OfficeSkins.Register();
            //DevExpress.UserSkins.BonusSkins.Register(); 
            DevExpress.XtraEditors.Controls.Localizer.Active = new DevExpress.LocalizationCHS.DevExpressXtraEditorsLocalizationCHS();
            DevExpress.XtraGrid.Localization.GridLocalizer.Active = new DevExpress.LocalizationCHS.DevExpressXtraGridLocalizationCHS();
            DevExpress.XtraPrinting.Localization.PreviewLocalizer.Active = new DevExpress.LocalizationCHS.DevExpressXtraPrintingLocalizationCHS();
            DevExpress.Skins.SkinManager.EnableFormSkins();
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            string proc = Process.GetCurrentProcess().ProcessName;
            Process[] processes = Process.GetProcessesByName(proc);
            if (processes.Length > 1)
            {
                MessageBox.Show("程序已经运行!","操作提示");
            }
            else
            {
                Application.Run(new MainForm());
            }
        }

 

posted @ 2010-07-23 22:04  deepwishly  阅读(199)  评论(0编辑  收藏  举报