zoukankan      html  css  js  c++  java
  • C#.NET WINFORM应用程序中控制应用程序只启动一次

    在Program.cs中添加如下代码:

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

    namespace ArresterSerialPort
    {
        static class Program
        {
            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            [STAThread]
            static void Main()
            {

                Boolean createdNew; //返回是否赋予了使用线程的互斥体初始所属权
                System.Threading.Mutex instance = new System.Threading.Mutex(true, "ArresterSerialPort", out createdNew); //同步基元变量
                if (createdNew) //赋予了线程初始所属权,也就是首次使用互斥体
                {
                    Application.EnableVisualStyles();
                    Application.SetCompatibleTextRenderingDefault(false);
                    Application.Run(new MainForm());
                    instance.ReleaseMutex();
                }
                else
                {
                    MessageBox.Show("已经启动了一个程序,请先退出", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    Application.Exit();
                }

                //Application.EnableVisualStyles();
                //Application.SetCompatibleTextRenderingDefault(false);
                //Application.Run(new ArresterMainForm());
            }
        }
    }

    http://www.tzwhx.com/newOperate/Html/1/12/121/4796.html

    防止程序运行多个实例的方法有多种,如:通过使用互斥量和进程名等.
    而我想要实现的是:在程序运行多个实例时激活的是第一个实例,使其获得焦点,并在前端显示.

    主要调用两个API函数:
    #region API函数
    /// <summary>
    /// 该函数设置由不同线程产生的窗口的显示状态。
    /// </summary>
    /// <param name="hWnd">窗口句柄</param>
    /// <param name="cmdShow">指定窗口如何显示。查看允许值列表,请查阅ShowWlndow函数的说明部分。</param>
    /// <returns>如果函数原来可见,返回值为非零;如果函数原来被隐藏,返回值为零。</returns>
    [DllImport("User32.dll")]
    private static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow);
    /// <summary> 
    /// 该函数将创建指定窗口的线程设置到前台,并且激活该窗口。键盘输入转向该窗口,并为用户改各种可视的记号。
    //  系统给创建前台窗口的线程分配的权限稍高于其他线程。
    /// </summary>
    /// <param name="hWnd">将被激活并被调入前台的窗口句柄。</param>
    /// <returns>如果窗口设入了前台,返回值为非零;如果窗口未被设入前台,返回值为零。</returns>
    [DllImport("User32.dll")]
    private static extern bool SetForegroundWindow(IntPtr hWnd);
    private  const  int  SW_HIDE  =  0; //隐藏 
    private  const  int  SW_SHOWNORMAL  =  1;  //Normal
    private  const  int  SW_SHOWMINIMIZED  =  2;  //最小化
    private  const  int  SW_SHOWMAXIMIZED  =  3;  //最大化
    private  const  int  SW_SHOWNOACTIVATE  =  4;  //激活
    private  const  int  SW_RESTORE  =  9;  //Restore
    private  const  int  SW_SHOWDEFAULT  =  10;  //Default
    #endregion

    #region 封装API函数
    private static void HandleRunningInstance(Process instance)
    {
    // 确保窗口没有被最小化或最大化
    ShowWindowAsync(instance.MainWindowHandle, SW_SHOWNOACTIVATE);
    // 设置真实例程为foreground  window 
    SetForegroundWindow(instance.MainWindowHandle);// 放到最前端
    }
    private static Process RunningInstance()
    {
    Process current = Process.GetCurrentProcess();
    Process[] processes = Process.GetProcessesByName(current.ProcessName);
    foreach (Process process in processes)
    {
    if (process.Id != current.Id)
    {
    // 确保例程从EXE文件运行
    if (Assembly.GetExecutingAssembly().Location.Replace("/", "\\") == current.MainModule.FileName)
    {
    return process;
    }
    }
    }
    return null;
    }
    #endregion

    以下是在Program.cs类如何调用:
    /// <summary>
    /// 应用程序的主入口点。
    /// </summary>
    [STAThread]
    static void Main(string[] args)
    {
    try
    {
    Process instance = RunningInstance();
    if (instance == null)
    {
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Application.Run(new MainForm);
    }
    else
    {
    HandleRunningInstance(instance);
    }
    catch(Exception e){}
    }

    http://blog.sina.com.cn/s/blog_61225ccc0100enxo.html

  • 相关阅读:
    给你一个长度为 n 的数组,其中只有一个数字出现了大于等于 n/2 次,问如何使用优秀的 时空复杂度快速找到这个数字。
    给定一个非空整数数组,除了某个元素只出现一次以外,其余每个元素均出现偶数次。找出那个只出现了一次的元素。
    python虚拟环境配置
    测试环境配置
    使用ELK Stack收集kubernetes集群内的应用日志
    vue 禁止遮罩层下的页面滑动
    vue 把 java 传过来的流文件 转成apk、xls等
    vue 中使用 webSocket 收发数据, 增加 " 心跳机制 " 保持连接.
    webstrom 根据当前编辑文件定位左侧目录
    MySQL 8.0新特性详解(转)
  • 原文地址:https://www.cnblogs.com/emanlee/p/1557379.html
Copyright © 2011-2022 走看看