DevExpress Winform使用单例运行程序方法和非DevExpress使用Mutex实现程序单实例运行且运行则激活窗体的方法
原文:DevExpress Winform使用单例运行程序方法和非DevExpress使用Mutex实现程序单实例运行且运行则激活窗体的方法
版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。
网上关于C#单例运行程序的方法都是比较简单,有些甚至是无法实现功能的,不知道他们试没试过就发帖,因为自己之前都是用第三方控件DevExpress,单例运行也是用它本身自带的一个方法,调用此方法需要引用DevExpress的DevExpress.DevAV.v17.1.Data.dll
-
static void Main()
-
{
-
var appName= Process.GetCurrentProcess().ProcessName;
-
using (DevExpress.Internal.DevAVDataDirectoryHelper.SingleInstanceApplicationGuard(appName, out exit))
-
{
-
if (exit)
-
return;
-
Application.EnableVisualStyles();
-
Application.SetCompatibleTextRenderingDefault(false);
-
Application.Run(new Form1());
-
}
-
}
以前懒,看到有现成的就拿现成的,也没去想具体如何实现,刚好有人问起,才决定去看看源码,发现DevExpress的这个方法原来是用了Mutex来实现的,整合了一下资料,重新写一个供C#桌面应用程序通用的单例运行方法,代码如下:
其中需要引用以下命名空间:
-
using System;
-
using System.Diagnostics;
-
using System.Drawing;
-
using System.Runtime.InteropServices;
-
using System.Security;
-
using System.Threading;
-
/// <summary>
-
/// 单实例应用程序--by涛神
-
/// </summary>
-
public class SingleInstanceApplication
-
{
-
/// <summary>
-
/// 判断应用是否运行
-
/// </summary>
-
/// <param name="processName"></param>
-
/// <param name="exit"></param>
-
/// <returns></returns>
-
public static IDisposable Guard(out bool exit)
-
{
-
Process currentProcess = Process.GetCurrentProcess();
-
var processName = currentProcess.ProcessName;
-
Mutex mutex = new Mutex(true, processName, out bool createNew);
-
if (createNew)
-
{
-
exit = false;
-
}
-
else
-
{
-
Process[] processesByName = Process.GetProcessesByName(currentProcess.ProcessName);
-
int num = 0;
-
while (num < (int)processesByName.Length)
-
{
-
Process process = processesByName[num];
-
if (process.Id == currentProcess.Id || !(process.MainWindowHandle != IntPtr.Zero))
-
{
-
num++;
-
}
-
else
-
{
-
WinApiHelper.SetForegroundWindow(process.MainWindowHandle);
-
WinApiHelper.RestoreWindowAsync(process.MainWindowHandle);
-
break;
-
}
-
}
-
exit = true;
-
}
-
return mutex;
-
}
-
private static class WinApiHelper
-
{
-
[SecuritySafeCritical]
-
public static bool IsMaxmimized(IntPtr hwnd)
-
{
-
WinApiHelper.Import.WINDOWPLACEMENT wINDOWPLACEMENT = new WinApiHelper.Import.WINDOWPLACEMENT();
-
if (!WinApiHelper.Import.GetWindowPlacement(hwnd, ref wINDOWPLACEMENT))
-
{
-
return false;
-
}
-
return wINDOWPLACEMENT.showCmd == WinApiHelper.Import.ShowWindowCommands.ShowMaximized;
-
}
-
-
[SecuritySafeCritical]
-
public static bool RestoreWindowAsync(IntPtr hwnd)
-
{
-
return WinApiHelper.Import.ShowWindowAsync(hwnd, (WinApiHelper.IsMaxmimized(hwnd) ? 3 : 9));
-
}
-
-
[SecuritySafeCritical]
-
public static bool SetForegroundWindow(IntPtr hwnd)
-
{
-
return WinApiHelper.Import.SetForegroundWindow(hwnd);
-
}
-
-
private static class Import
-
{
-
[DllImport("user32.dll", CharSet = CharSet.None, ExactSpelling = false)]
-
public static extern bool GetWindowPlacement(IntPtr hWnd, ref WinApiHelper.Import.WINDOWPLACEMENT lpwndpl);
-
-
[DllImport("user32.dll", CharSet = CharSet.None, ExactSpelling = false)]
-
public static extern bool SetForegroundWindow(IntPtr hWnd);
-
-
[DllImport("user32.dll", CharSet = CharSet.None, ExactSpelling = false)]
-
public static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
-
-
public enum ShowWindowCommands
-
{
-
Hide,
-
Normal,
-
ShowMinimized,
-
ShowMaximized,
-
ShowNoActivate,
-
Show,
-
Minimize,
-
ShowMinNoActive,
-
ShowNA,
-
Restore,
-
ShowDefault,
-
ForceMinimize
-
}
-
-
public struct WINDOWPLACEMENT
-
{
-
public int length;
-
-
public int flags;
-
-
public WinApiHelper.Import.ShowWindowCommands showCmd;
-
-
public Point ptMinPosition;
-
-
public Point ptMaxPosition;
-
-
public Rectangle rcNormalPosition;
-
}
-
}
-
}
-
}
调用的demo如下,亲测可以,若无法实现的请留言讨论(本文章是原创文章,转载请标明,盗版必究,觉得文章对你帮助的可以点个赞表示支持一下,谢谢):
-
static void Main()
-
{
-
using(SingleInstanceApplication.Guard(out bool exit))
-
{
-
if (exit)
-
return;
-
Application.EnableVisualStyles();
-
Application.SetCompatibleTextRenderingDefault(false);
-
Application.Run(new Form1());
-
}
-
}
DevExpress Winform使用单例运行程序方法和非DevExpress使用Mutex实现程序单实例运行且运行则激活窗体的方法的更多相关文章
- java——多线程——单例模式的static方法和非static方法是否是线程安全的?
单例模式的static方法和非static方法是否是线程安全的? 答案是:单例模式的static方法和非static方法是否是线程安全的,与单例模式无关.也就说,如果static方法或者非static ...
- synchronized 修饰在 static方法和非static方法的区别
Java中synchronized用在静态方法和非静态方法上面的区别 在Java中,synchronized是用来表示同步的,我们可以synchronized来修饰一个方法.也可以synchroniz ...
- Java中synchronized 修饰在static方法和非static方法的区别
[问题描述]关于Java中synchronized 用在实例方法和对象方法上面的区别 [问题分析]大家都知道,在Java中,synchronized 是用来表示同步的,我们可以synchronized ...
- 关于C#单例Singleton的看法和使用
首先明白一点,什么是单例模式? 单例模式是指一个类在一个应用程序运行时仅仅实例化一次,以后所有的调用都使用第一次实例化的对象,是应用程序级别的,与session,用户等无关,它比全局参数或静态类方式更 ...
- iOS——Swift开发中的单例设计模式(摘译,非原创)
最近在开发一个小的应用,遇到了一些Objective-c上面常用的单例模式,但是swift上面还是有一定区别的,反复倒来倒去发现不能按常理(正常的oc to swift的方式)出牌,因此搜索了一些帖子 ...
- java static成员变量方法和非static成员变量方法的区别
这里的普通方法和成员变量是指,非静态方法和非静态成员变量首先static是静态的意思,是修饰符,可以被用来修饰变量或者方法. static成员变量有全局变量的作用 非static成员变量则 ...
- 表单元素的submit()方法和onsubmit事件
1.表单元素中出现了name="submit"的元素 2.elemForm.submit();不会触发表单的onsubmit事件 3.动态创建表单时遇到的问题 表单元素拥有subm ...
- 表单元素的submit()方法和onsubmit事件(转)
1.表单元素中出现了name="submit"的元素 2.elemForm.submit();不会触发表单的onsubmit事件 3.动态创建表单时遇到的问题 表单元素拥有subm ...
- static方法和非static方法的区别
●生命周期(Lifecycle):静态方法(Static Method)与静态成员变量一样,属于类本身,在类装载的时候被装载到内存(Memory),不自动进行销毁,会一直存在于内存中,直到JVM关闭. ...
随机推荐
- 搭建Keepalived+LNMP架构web动态博客 实现高可用与负载均衡
环境准备: 192.168.193.80 node1 192.168.193.81 node2 关闭防火墙 [root@node1 ~]# systemctl stop firewalld #两台都 ...
- 动态规划—distinct-subsequences
题目: Given a string S and a string T, count the number of distinct subsequences of T in S. A subseque ...
- luogu4061 大吉大利,晚上吃鸡!
链接 最短路径\(dag\),一道好题. 题目大意:求一张图中满足下列要求的点对\((i,j)\)数量: 所有最短路径必定会经过 \(i\) 点和 \(j\) 点中的任意一点. 不存在一条最短路同时经 ...
- Thread类与Runnable接口
Runnable 先看看源码中对Runnable的声明 @FunctionalInterface public interface Runnable { /** * When an object im ...
- python tkinter画圆
x0=150 #圆心横坐标 y0=100 #圆心纵坐标 canvas.create_oval(x0-10,y0-10,x0+10,y0+10) #圆外矩形左上角与右下角坐标 canv ...
- QTimer不能同时使用两个,用QTimerEvent (QT)
最近写程序的时候有个界面想定两个QTimer定时器,不同时间干不同的事: QTimer *timer1 = new QTimer(this); QTimer *timer2 = new QTimer( ...
- idea返回git上历史版本
1.首先找到之前想要返回得版本号 2.直接下载此版本号即可 在这里填入1步骤得版本号即可检出,其实这个检出利时版本和检出其他分支是同一个道理
- 微信公众号号开发(Java)
参考:http://www.cnblogs.com/fengzheng/p/5023678.html http://www.cnblogs.com/xdp-gacl/p/5151857.html 一: ...
- swan.after
解释: swan.after可以拦截所有当前运行小程序对于API的调用,默认传入function时,只在API函数调用的返回阶段拦截.如果传入Object,则可以选择拦截的阶段(例如: 返回阶段.回调 ...
- Xcode编辑器之快捷键的使用
一,快捷键图标 图标 键盘 ⌘ Command ⌃ Control ⌥ Option ⇧ Shift 二, 常用快捷键 文件快捷键 快捷键 键盘 描述 ⌘N command + N 新文件 ⇧⌘N ...