惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Security Archives - TechRepublic
Security Archives - TechRepublic
I
InfoQ
阮一峰的网络日志
阮一峰的网络日志
云风的 BLOG
云风的 BLOG
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
AWS News Blog
AWS News Blog
S
SegmentFault 最新的问题
T
Tailwind CSS Blog
The Hacker News
The Hacker News
GbyAI
GbyAI
P
Palo Alto Networks Blog
博客园 - 三生石上(FineUI控件)
Y
Y Combinator Blog
Stack Overflow Blog
Stack Overflow Blog
博客园 - Franky
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Cyberwarzone
Cyberwarzone
H
Help Net Security
S
Securelist
月光博客
月光博客
博客园 - 【当耐特】
T
Threatpost
T
Tenable Blog
G
GRAHAM CLULEY
博客园 - 司徒正美
I
Intezer
MyScale Blog
MyScale Blog
T
Threat Research - Cisco Blogs
P
Privacy & Cybersecurity Law Blog
The GitHub Blog
The GitHub Blog
C
CERT Recently Published Vulnerability Notes
T
Tor Project blog
Google DeepMind News
Google DeepMind News
C
Cybersecurity and Infrastructure Security Agency CISA
罗磊的独立博客
腾讯CDC
P
Privacy International News Feed
博客园_首页
The Cloudflare Blog
Cisco Talos Blog
Cisco Talos Blog
A
About on SuperTechFans
V
Vulnerabilities – Threatpost
A
Arctic Wolf
B
Blog RSS Feed
Recorded Future
Recorded Future
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google DeepMind News
Google DeepMind News
S
Security Affairs
Microsoft Security Blog
Microsoft Security Blog
L
LangChain Blog

博客园 - 曾伟

不干胶、热敏打印 wpf 滚屏数据显示 net core quartz调度 warp打包 nssm部署到windowsservice c# webapi 过滤器token、sign认证、访问日志 c# NPOI文件操作 c# ini文件操作 WCF多种调用方式兼容 网页打印javascript:window.print() window.showModalDialog弹出对话框刷新问题 ASP.NET] 选择文件夹的对话框 常用SQL语句书写技巧 IIS6 MMC检测到此管理单元发生一个错误,建议您关闭并重新启动mmc 在上传文件时限制上传文件的大小,并捕捉超过文件大小限制的 - 曾伟 - 博客园 Lucene的例子 javascript 获取标签具体位置 - 曾伟 - 博客园 JavaScript实现类,有多种方法。 Javascript实现截图功能(代码) 终端服务器超出了最大允许连接数 DBCC CHECKDB 数据库或表修复
控制同一exe程序打开多次 - 曾伟 - 博客园
曾伟 · 2010-12-31 · via 博客园 - 曾伟

1.新建一个SingleInstance类:

using System;
using System.Collections.Generic;
using System.Text;

using System.Runtime.InteropServices;
using System.Diagnostics;

namespace NetChange
{
    class SingleInstance
    {
        [DllImport("User32.dll")]
        private static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow);
        [DllImport("User32.dll")]
        private static extern bool SetForegroundWindow(IntPtr hWnd);

        private const int WS_SHOWNORMAL = 1;

        public static Process GetRunningInstance()
        {
            Process currentProcess = Process.GetCurrentProcess();
            string currentFileName = currentProcess.MainModule.FileName;
            Process[] processes = Process.GetProcessesByName(currentProcess.ProcessName);
            foreach (Process process in processes)
            {
                if (process.MainModule.FileName == currentFileName)
                {
                    if (process.Id != currentProcess.Id)
                        return process;
                }
            }
            return null;
        }


       public static bool HandleRunningInstance(Process instance)
       {

          ShowWindowAsync(instance.MainWindowHandle, WS_SHOWNORMAL);
          return SetForegroundWindow(instance.MainWindowHandle);
       }


      public static bool HandleRunningInstance()
     {
        Process p = GetRunningInstance();
        if (p != null)
        {
                HandleRunningInstance(p);
                return true;
        }
        return false;
     }

    }  
}
2.在program.cs 修改代码如下:

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

namespace NetChange
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            //===定义SingleInstance类,实现应用运行唯一性判断==
            if (SingleInstance.HandleRunningInstance() == false)
            {
                Application.Run(new Form1());
            }
            else {
                 Application.Exit();
            }
            //===========
            //Application.Run(new Form1());
        }
    }
}