zoukankan      html  css  js  c++  java
  • c# 控制职能运行单一实例,再次运行显示已经运行的实例

    有这么个需求,软件只能运行一个实例,软件运行后可以让其隐藏运行

    再次运行这个软件的时候就让正在运行的实例显示出来

    =================================

    当软件隐藏后没办法拿到句柄

    于是只有第一次运行的时候讲句柄保存下来,于是有了下面的

     1  private void HideForm()
     2         {
     3             string handlestr = System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle.ToInt32().ToString();
     4             string path = Application.StartupPath + "\Handle";
     5             if (!File.Exists(path))
     6             {
     7                 File.Create(path).Close();
     8             }
     9             using (StreamWriter writer = new StreamWriter(path, false))
    10             {
    11                 writer.Write(handlestr);
    12             }
    13             this.Hide();
    14         }
    View Code

    哪里需要隐藏就调用

    下面是控制

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Windows.Forms;
     5 using System.Diagnostics;
     6 using System.Runtime.InteropServices;
     7 using System.IO;
     8 
     9 namespace NIKE.Client
    10 {
    11     static class Program
    12     {
    13         /// <summary>
    14         /// 应用程序的主入口点。
    15         /// </summary>
    16         [STAThread]
    17         static void Main()
    18         {
    19             //StaticPass.cf = new ClientForm();
    20             //Application.EnableVisualStyles();
    21             //Application.SetCompatibleTextRenderingDefault(false);
    22             //Application.Run(new FormMain());
    23             bool ret;
    24             System.Threading.Mutex mutex = new System.Threading.Mutex(true, Application.ProductName, out ret);
    25             if (ret)
    26             {
    27                 System.Windows.Forms.Application.EnableVisualStyles();   //这两行实现   XP   可视风格   
    28                 //System.Windows.Forms.Application.DoEvents();             //这两行实现   XP   可视风格   
    29                 Application.SetCompatibleTextRenderingDefault(false);
    30                 System.Windows.Forms.Application.Run(new FormMain());
    31                 //   Main   为你程序的主窗体,如果是控制台程序不用这句   
    32                 mutex.ReleaseMutex();
    33             }
    34             else
    35             {
    36 
    37                 string path = Application.StartupPath + "\Handle";
    38                 int handle = 0;
    39                 using (StreamReader reader = new StreamReader(path))
    40                 {
    41                     handle = int.Parse(reader.ReadToEnd());
    42                 }
    43                 IntPtr i = new IntPtr(handle);
    44                 bool isok = ShowWindow(i, 1);
    45                 SetForegroundWindow(i);
    46                 //  SetForegroundWindow(hWnd);
    47                 // MessageBox.Show(null, "有一个和本程序相同的应用程序已经在运行,请不要同时运行多个本程序。
    
    本次打开无效", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Warning);
    48                 //   提示信息,可以删除。   
    49                 Application.Exit();//退出程序   
    50             }
    51         }
    52 
    53         [DllImport("user32.dll")]
    54         private static extern bool SetForegroundWindow(IntPtr hWnd);
    55         [DllImport("user32.dll")]
    56         private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);      
    57         private const int SW_SHOWNORMAL = 1;
    58        
    59     }
    60 }
    View Code
  • 相关阅读:
    推荐系统 蒋凡译 第一章 引言 读书笔记
    神经网络与深度学习 邱锡鹏 第5章 卷积神经网络 读书笔记
    神经网络与深度学习 邱锡鹏 第4章 前馈神经网络 读书笔记
    神经网络与深度学习 邱锡鹏 第3章 线性模型 读书笔记
    神经网络与深度学习 邱锡鹏 第2章 机器学习概述 读书笔记
    神经网络与深度学习 邱锡鹏 第1章 绪论 作业
    神经网络与深度学习 邱锡鹏 第1章 绪论 读书笔记
    算法笔记 上机训练实战指南 第13章 专题扩展 学习笔记
    算法笔记 第13章 专题扩展 学习笔记
    算法笔记 上机训练实战指南 第11章 提高篇(5)--动态规划专题 学习笔记
  • 原文地址:https://www.cnblogs.com/bfyx/p/4178085.html
Copyright © 2011-2022 走看看