码迷,mamicode.com
首页 > Windows程序 > 详细

C# 避免程序重复启动

时间:2018-03-17 10:56:18      阅读:416      评论:0      收藏:0      [点我收藏+]

标签:visual   thread   避免   icon   reading   else   sum   mes   log   

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Threading;

namespace WindowsFormsApplication1
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            bool ret;
            Mutex mutex = new Mutex(true, Application.ProductName, out   ret);
            if (ret)
            {
                Application.Run(new Form1());
            }
            else
            {
                MessageBox.Show("该程序已经启动", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            
        }
    }
}

应用程序多次启动会因为资源占用等问题对程序的正常运行产生影响,在某些情况下需要对程序的启动次数进行限制。红色部分代码的作用是避免程序重复启动。

C# 避免程序重复启动

标签:visual   thread   避免   icon   reading   else   sum   mes   log   

原文地址:https://www.cnblogs.com/LicwStack/p/8587352.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!