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

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

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

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

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

  private void HideForm()
{
string handlestr = System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle.ToInt32().ToString();
string path = Application.StartupPath + "\\Handle";
if (!File.Exists(path))
{
File.Create(path).Close();
}
using (StreamWriter writer = new StreamWriter(path, false))
{
writer.Write(handlestr);
}
this.Hide();
}

哪里需要隐藏就调用

下面是控制

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.IO; namespace NIKE.Client
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
//StaticPass.cf = new ClientForm();
//Application.EnableVisualStyles();
//Application.SetCompatibleTextRenderingDefault(false);
//Application.Run(new FormMain());
bool ret;
System.Threading.Mutex mutex = new System.Threading.Mutex(true, Application.ProductName, out ret);
if (ret)
{
System.Windows.Forms.Application.EnableVisualStyles(); //这两行实现 XP 可视风格
//System.Windows.Forms.Application.DoEvents(); //这两行实现 XP 可视风格
Application.SetCompatibleTextRenderingDefault(false);
System.Windows.Forms.Application.Run(new FormMain());
// Main 为你程序的主窗体,如果是控制台程序不用这句
mutex.ReleaseMutex();
}
else
{ string path = Application.StartupPath + "\\Handle";
int handle = ;
using (StreamReader reader = new StreamReader(path))
{
handle = int.Parse(reader.ReadToEnd());
}
IntPtr i = new IntPtr(handle);
bool isok = ShowWindow(i, );
SetForegroundWindow(i);
// SetForegroundWindow(hWnd);
// MessageBox.Show(null, "有一个和本程序相同的应用程序已经在运行,请不要同时运行多个本程序。\n\n本次打开无效", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Warning);
// 提示信息,可以删除。
Application.Exit();//退出程序
}
} [DllImport("user32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")]
private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
private const int SW_SHOWNORMAL = ; }
}

c# 控制职能运行单一实例,再次运行显示已经运行的实例的更多相关文章

  1. JVM学习001通过实例总结Java虚拟机的运行机制

    JVM学习(1)——通过实例总结Java虚拟机的运行机制-转载http://www.cnblogs.com/kubixuesheng/p/5199200.html 文章转载自:http://www.c ...

  2. Java-Runoob-高级教程-实例-环境设置实例:4.Java 实例 – 如何查看当前 Java 运行的版本?

    ylbtech-Java-Runoob-高级教程-实例-环境设置实例:4.Java 实例 – 如何查看当前 Java 运行的版本? 1.返回顶部 1. Java 实例 - 如何查看当前 Java 运行 ...

  3. 在 Linux 实例上自动安装并运行 VNC Server

    原文网址:https://help.aliyun.com/knowledge_detail/41181.html?spm=5176.8208715.110.11.4c184ae8mlC7Yy 您可以使 ...

  4. Java线程面试题:子线程先运行 2 次,然后主线程运行 4 次,如此反复运行 3 次

    package thread; /** * 需求:线程编程:子线程先运行 2 次,然后主线程运行 4 次,如此反复运行 3 次. * @author zhongfg * @date 2015-06-1 ...

  5. Linux显示所有运行中的进程

    Linux显示所有运行中的进程 youhaidong@youhaidong-ThinkPad-Edge-E545:~$ ps aux | less USER PID %CPU %MEM VSZ RSS ...

  6. C# 获取当前服务器运行程序的根目录,获取当前运行程序物理路径

    C# 获取当前服务器运行程序的根目录,获取当前运行程序物理路径 string tmpRootDir = AppDomain.CurrentDomain.BaseDirectory;//获得当前服务器程 ...

  7. 未能加载文件或程序集“BLL”或它的某一个依赖项。生成此程序集的运行时比当前加载的运行时新,无法加载此程序集。

    今天使用VS2012创建项目的时候,考虑到项目中代码的重用性以及清晰简洁性,搭建了一个三层架构,但是在项目运行的时候,总是报错: “未能加载文件或程序集“BLL”或它的某一个依赖项.生成此程序集的运行 ...

  8. Docker在Linux上运行NetCore系列(一)配置运行DotNetCore控制台

    转发请注明此文章作者与路径,请尊重原著,违者必究. 系列文章:https://www.cnblogs.com/alunchen/p/10121379.html 本篇文章操作系统信息 Linux:ubu ...

  9. sublime将python的运行结果在命令行显示

    sublime将python的运行结果在命令行显示 为什么这么折腾? 因为每次查看输出结果都要上下拖动窗口,很烦. 将build system修改为 { "cmd": [" ...

  10. Singer 学习七 运行&&开发taps、targets (二 targets 运行说明)

    接上文: Singer 学习六 运行&&开发taps.targets (一 taps 运行说明) 说明target 需要tap 进行配合运行,所以需要了解tap 的使用 运行targe ...

随机推荐

  1. MVC基本概念和流程

    MVC基本概念和流程 MVC的概念 Model(模型):包含数据和行为.不过现在一般都分离开来:Value Object(数据) 和 服务层(行为). View(视图):负责进行模型的展示,一般就是展 ...

  2. 解决:&lt;net.sf.ehcache.util.UpdateChecker&gt; : New update(s) found: 2.6.5

    由于该项目采用ehcache,所以tomcat每次登录你开始打印net.sf.ehcache.util.UpdateChecker doCheck 一旦有没有特别关注.从今天开始 Tomcat 什么时 ...

  3. Unity插件之NGUI学习(8)—— Table和NGUI尺寸转换为世界坐标系尺寸

    依据 Unity插件之NGUI学习(2),创建一个UI Root,在UI Root下创建一个Texture作为背景图,并设置图片,在Wiget下调整大小:然后在UI Root下再创建一个Panel. ...

  4. linux_ssky-keygen + ssh-copy-id 无密码登陆远程LINUX主机

    使用下例中ssky-keygen和ssh-copy-id,仅需通过3个步骤的简单设置而无需输入密码就能登录远程Linux主机. ssh-keygen 创建公钥和密钥. ssh-copy-id 把本地主 ...

  5. BZOJ 1355 Baltic2009 Radio Transmission KMP算法

    标题效果:给定一个字符串,求最小周期节(不能整除) 示例Hint这是错误的忽略了就好了 环路部分应该是cab 这个称号充分利用KMP在next自然阵列,那是,n-next[n]它表示一个循环节 POJ ...

  6. VTune使用amplxe-cl进行Hardware Event-based Sampling Analysis 0分析

    于BASH正在使用VTune进行Hardware Event-based Sampling Analysis 0分析: 结果(部分)例如以下: 版权声明:本文博客原创文章.博客,未经同意,不得转载.

  7. Utility Classes Are Evil

    原文地址:http://alphawang.com/blog/2014/09/utility-classes-are-evil/ This post is a summary of this arti ...

  8. PHP经验——获得PHP版本信息及版本比较

    原文:PHP经验--获得PHP版本信息及版本比较 偶然看到别人写的一句代码: <?php if (version_compare("5.2", PHP_VERSION, &q ...

  9. UpdateModel方法

    WebForm 对 MVC 说:能否借你的UpdateModel方法来用用? 背景 ASP.NET MVC的Controller有个很不错的方法:UpdataModel (相对应的还有TryUpdat ...

  10. 自制 Word、Excel 批转 PDF 工具

    原文:自制 Word.Excel 批转 PDF 工具 目前做金融业的项目,该公司每天会产生很多 Word.Excel 文档,需要大量地转换为 PDF,除了自己保存外,也要给金融主管机构作为备份.由于文 ...