前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >c# 控制职能运行单一实例,再次运行显示已经运行的实例

c# 控制职能运行单一实例,再次运行显示已经运行的实例

作者头像
冰封一夏
发布2019-09-11 15:34:30
5170
发布2019-09-11 15:34:30
举报

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

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

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

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

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

代码语言:javascript
复制
 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         }

哪里需要隐藏就调用

下面是控制

代码语言:javascript
复制
 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, "有一个和本程序相同的应用程序已经在运行,请不要同时运行多个本程序。\n\n本次打开无效", 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 }
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2014-12-22 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档