c# 控制职能运行单一实例,再次运行显示已经运行的实例
有这么个需求,软件只能运行一个实例,软件运行后可以让其隐藏运行
再次运行这个软件的时候就让正在运行的实例显示出来
=================================
当软件隐藏后没办法拿到句柄
于是只有第一次运行的时候讲句柄保存下来,于是有了下面的
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# 控制职能运行单一实例,再次运行显示已经运行的实例的更多相关文章
- JVM学习001通过实例总结Java虚拟机的运行机制
JVM学习(1)——通过实例总结Java虚拟机的运行机制-转载http://www.cnblogs.com/kubixuesheng/p/5199200.html 文章转载自:http://www.c ...
- Java-Runoob-高级教程-实例-环境设置实例:4.Java 实例 – 如何查看当前 Java 运行的版本?
ylbtech-Java-Runoob-高级教程-实例-环境设置实例:4.Java 实例 – 如何查看当前 Java 运行的版本? 1.返回顶部 1. Java 实例 - 如何查看当前 Java 运行 ...
- 在 Linux 实例上自动安装并运行 VNC Server
原文网址:https://help.aliyun.com/knowledge_detail/41181.html?spm=5176.8208715.110.11.4c184ae8mlC7Yy 您可以使 ...
- Java线程面试题:子线程先运行 2 次,然后主线程运行 4 次,如此反复运行 3 次
package thread; /** * 需求:线程编程:子线程先运行 2 次,然后主线程运行 4 次,如此反复运行 3 次. * @author zhongfg * @date 2015-06-1 ...
- Linux显示所有运行中的进程
Linux显示所有运行中的进程 youhaidong@youhaidong-ThinkPad-Edge-E545:~$ ps aux | less USER PID %CPU %MEM VSZ RSS ...
- C# 获取当前服务器运行程序的根目录,获取当前运行程序物理路径
C# 获取当前服务器运行程序的根目录,获取当前运行程序物理路径 string tmpRootDir = AppDomain.CurrentDomain.BaseDirectory;//获得当前服务器程 ...
- 未能加载文件或程序集“BLL”或它的某一个依赖项。生成此程序集的运行时比当前加载的运行时新,无法加载此程序集。
今天使用VS2012创建项目的时候,考虑到项目中代码的重用性以及清晰简洁性,搭建了一个三层架构,但是在项目运行的时候,总是报错: “未能加载文件或程序集“BLL”或它的某一个依赖项.生成此程序集的运行 ...
- Docker在Linux上运行NetCore系列(一)配置运行DotNetCore控制台
转发请注明此文章作者与路径,请尊重原著,违者必究. 系列文章:https://www.cnblogs.com/alunchen/p/10121379.html 本篇文章操作系统信息 Linux:ubu ...
- sublime将python的运行结果在命令行显示
sublime将python的运行结果在命令行显示 为什么这么折腾? 因为每次查看输出结果都要上下拖动窗口,很烦. 将build system修改为 { "cmd": [" ...
- Singer 学习七 运行&&开发taps、targets (二 targets 运行说明)
接上文: Singer 学习六 运行&&开发taps.targets (一 taps 运行说明) 说明target 需要tap 进行配合运行,所以需要了解tap 的使用 运行targe ...
随机推荐
- linux runtime pm在深入了解的机制
一:runtime机构简介 何为runtime机制?也就是系统在非睡眠状态,设备在空暇时能够进入runtime suspend状态同一时候不依赖系统wake_lock机制.非空暇时运行runtime ...
- ScrollView 嵌套ListView 幻灯冲突,和显示不全
import android.content.Context; import android.util.AttributeSet; import android.widget.ListView; /* ...
- 虚拟WiFi
转载用WinForm写一个虚拟WiFi助手玩玩(附源码) 这早不是什么新鲜的东西了,同类软件已经有很多,但不是收费就是有广告,在学校的时候就想自已写一个了,但那时候啥也没学,对C的掌握程度也就是定 ...
- 操作jQuery集合搜索父元素
搜索父元素 1.1parents()方法 parents()方法用于获取u当前匹配元素集合中的每个元素的祖先元素,根据需要还可以使用一个选择器进行筛选parents([selector]) 其中sel ...
- Hibeernate中的两种分页方式
1. return getHibernateTemplate().executeFind(new HibernateCallback() { public Object doInHibernate(S ...
- Ubuntu自己主动搭建VPN Server - PPTP的Shell脚本
#!/bin/bash if [ "$UID" != "0" ]; then echo "please use sudo to run $0" ...
- C编程的指针涛 ---第九笔记
//这里说的是一个指针,指向算法的应用 //直接排序 //每个排序算法是指针指向的每个元件的特性的方便的交流 //这里的基本思想是,处理的记录的排序n - 1第二选择. //第i次操作选择i大(小)的 ...
- 系统预定义委托与Lambda表达式
NET中那些所谓的新语法之三:系统预定义委托与Lambda表达式 开篇:在上一篇中,我们了解了匿名类.匿名方法与扩展方法等所谓的新语法,这一篇我们继续征程,看看系统预定义委托(Action/Fun ...
- 图解IntelliJ IDEA 13版本对Android SQLite数据库的支持
IntelliJ IDEA 13版本的重要构建之一是支持Android程序开发.当然对Android SQLite数据库的支持也就成为了Android开发者对IntelliJ IDEA 13版本的绝对 ...
- C#用Open与Add方法打开word文档的区别
C#打开word文档常用有两种方法:Add与Open. Microsoft.Office.Interop.Word._Document doc = (Document)appWord.Document ...