zoukankan      html  css  js  c++  java
  • C# .net防止一个程序(WinForm)重复运行的方法。

    在写一些服务型的软件的时候,你可能不希望一个操作系统里有两个副本在运行,这样也许会扰乱你的操作。这时,你就需要限制程序的副本。下面的这个方法,很简单的就可以实现上述功能。

    using System;
    using System.Collections.Generic;
    using System.Windows.Forms;
    using System.Diagnostics;

    namespace TestProcessCount
    {
        static class Program
        {
            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            [STAThread]
            static void Main()
            {
                int processCount = 0;
                Process[] pa = Process.GetProcesses();//获取当前进程数组。
                foreach (Process PTest in pa)
                {
                    if (PTest.ProcessName == Process.GetCurrentProcess().ProcessName)
                    {
                        processCount += 1;
                    }
                }
                if (processCount > 1)
                {
                    //如果程序已经运行,则给出提示。并退出本进程。
                    DialogResult dr;
                    dr = MessageBox.Show( Process.GetCurrentProcess().ProcessName+"程序已经在运行!", "退出程序", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return; //Exit;
                  
                }
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new frmBrowser());
            }
        }
    }

  • 相关阅读:
    【bzoj3676】[Apio2014]回文串 —— 回文自动机的学习
    树链剖分求LCA
    读入输出优化
    【bzoj3124】[Sdoi2013]直径
    【codevs2183】匹配字符串
    【codevs2011】【LNOI2013】最小距离之和
    【codevs1306】广播操的游戏
    【hdu3966】Aragorn's Story
    【hdu3518】Boring counting
    C++-HDU3400-Line belt[三分]
  • 原文地址:https://www.cnblogs.com/chenbg2001/p/1525433.html
Copyright © 2011-2022 走看看