C# 避免程序重復啟動


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

應用程序多次啟動會因為資源占用等問題對程序的正常運行產生影響,在某些情況下需要對程序的啟動次數進行限制。紅色部分代碼的作用是避免程序重復啟動。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM