---
title: "只能启动单个程序"
author: "devilhand"
date: "2012-05-04T12:51:34Z"
tags: []
categories: ["编程语言"]
slug: "untitled"
draft: false
lastmod: "2012-05-04T12:51:34Z"
id: 1511224
url: "https://devilhand.iteye.com/blog/1511224"
---

```
 bool ret;
            System.Threading.Mutex m = new System.Threading.Mutex(true, Application.ProductName, out   ret);
            if (ret)
            {
                System.Windows.Forms.Application.EnableVisualStyles();   //这两行实现   XP   可视风格   
                System.Windows.Forms.Application.DoEvents();

                ConfigurationManager.RefreshSection("appSettings");
                string storeId = ConfigurationManager.AppSettings["StoreID"];
                if (string.IsNullOrEmpty(storeId.Trim()))
                {
                    FrmInputStoreMsg frm = new FrmInputStoreMsg();
                    frm.ShowDialog();
                    if (frm.IsSucceed)
                    {
                        Application.Run(new FrmMain());
                    }
                }
                else
                {
                    Application.Run(new FrmMain());
                }

                m.ReleaseMutex();
            }
            else
            {
                MessageBox.Show(null, "有一个和本程序相同的应用程序已经在运行，请不要同时运行多个本程序。\n\n这个程序即将退出。", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                // 提示信息，可以删除。   
                Application.Exit();//退出程序   
            }
```