zoukankan
html css js c++ java
C#FORM只允许启动一个进程
SingleInstance类
Code
using
System;
using
System.Collections.Generic;
using
System.Text;
using
System.Threading;
using
System.Reflection;
using
System.Runtime.InteropServices;
//
win API 引用
using
System.Diagnostics;
//
Process
namespace
QueuerServer
{
/**/
///
<summary>
///
使应用程序仅运行一个实例。
///
</summary>
static
class
SingleInstance
{
[DllImport(
"
User32.dll
"
)]
private
static
extern
bool
ShowWindowAsync(IntPtr hWnd,
int
cmdShow);
[DllImport(
"
User32.dll
"
)]
private
static
extern
bool
SetForegroundWindow(IntPtr hWnd);
private
static
Mutex mutex
=
null
;
public
static
bool
CreateMutex()
{
return
CreateMutex(Assembly.GetEntryAssembly().FullName);
}
public
static
bool
CreateMutex(
string
name)
{
bool
result
=
false
;
mutex
=
new
Mutex(
true
, name,
out
result);
return
result;
}
public
static
void
ReleaseMutex()
{
if
(mutex
!=
null
)
{
mutex.Close();
}
}
/**/
///
<summary>
///
ShowWindow() Commands
///
</summary>
private
const
int
SW_HIDE
=
0
;
private
const
int
SW_SHOWNORMAL
=
1
;
private
const
int
SW_NORMAL
=
1
;
private
const
int
SW_SHOWMINIMIZED
=
2
;
private
const
int
SW_SHOWMAXIMIZED
=
3
;
private
const
int
SW_MAXIMIZE
=
3
;
private
const
int
SW_SHOWNOACTIVATE
=
4
;
private
const
int
SW_SHOW
=
5
;
private
const
int
SW_MINIMIZE
=
6
;
private
const
int
SW_SHOWMINNOACTIVE
=
7
;
private
const
int
SW_SHOWNA
=
8
;
private
const
int
SW_RESTORE
=
9
;
private
const
int
SW_SHOWDEFAULT
=
10
;
private
const
int
SW_FORCEMINIMIZE
=
11
;
private
const
int
SW_MAX
=
11
;
/**/
///
<summary>
///
激活同Instance窗体
///
</summary>
///
<param name="instance"></param>
///
<returns></returns>
public
static
bool
HandleRunningInstance(Process instance)
{
//
确保窗口没有被最小化或最大化
ShowWindowAsync(instance.MainWindowHandle, SW_MAXIMIZE);
//
设置为foreground window
return
SetForegroundWindow(instance.MainWindowHandle);
}
public
static
bool
HandleRunningInstance()
{
Process p
=
GetRunningInstance();
if
(p
!=
null
)
{
HandleRunningInstance(p);
return
true
;
}
return
false
;
}
/**/
///
<summary>
///
获取应用程序进程实例,如果没有匹配进程,返回Null值
///
</summary>
///
<returns></returns>
public
static
Process GetRunningInstance()
{
Process currentProcess
=
Process.GetCurrentProcess();
//
获取当前进程
//
获取当前运行程序完全限定名
string
currentFileName
=
currentProcess.MainModule.FileName;
//
获取进程名为ProcessName的Process数组。
Process[] processes
=
Process.GetProcessesByName(currentProcess.ProcessName);
//
遍历有相同进程名称正在运行的进程
foreach
(Process process
in
processes)
{
if
(process.MainModule.FileName
==
currentFileName)
{
if
(process.Id
!=
currentProcess.Id)
//
根据进程ID排除当前进程
return
process;
//
返回已运行的进程实例
}
}
return
null
;
}
}
}
主工程中:
Program
namespace
QueuerServer
{
static
class
Program
{
private
static
MainForm m_MainForm;
public
static
MainForm MainForm
{
get
{
return
Program.m_MainForm; }
}
/**/
///
<summary>
///
应用程序的主入口点。
///
</summary>
[STAThread]
static
void
Main(
string
[] args)
{
#region
bool
autoStart
=
false
;
bool
hide
=
false
;
bool
service
=
false
;
foreach
(
string
arg
in
args)
{
//
MessageBox.Show(arg);
string
command
=
arg.Trim().ToLower();
//
if(arg.Trim().ToLower().Equals("-autostart"))
switch
(command)
{
case
"
-autostart
"
:
autoStart
=
true
;
break
;
case
"
-hide
"
:
hide
=
true
;
break
;
case
"
-service
"
:
service
=
true
;
break
;
default
:
break
;
}
}
#endregion
//
使应用程序只能生成一个进程
if
(SingleInstance.CreateMutex())
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(
false
);
RunMainForm(autoStart, hide, service);
SingleInstance.ReleaseMutex();
}
else
//
若已经运行了一个进程,则激活该进程。
{
//
MessageBox.Show("程序已经运行!");
SingleInstance.HandleRunningInstance();
}
//
Application.EnableVisualStyles();
//
Application.SetCompatibleTextRenderingDefault(false);
//
Application.Run(new MainForm(autoStart, hide, service));
}
private
static
void
RunMainForm(
bool
autoStart,
bool
hide,
bool
service)
{
try
{
CreateMainForm(autoStart, hide, service);
}
catch
(Exception e)
{
if
(DialogResult.OK
==
MessageBox.Show(e.Message,
"
应用程序错误
"
, MessageBoxButtons.OKCancel))
{
//
这是一个重新启动机制,但是丢失全部应用程序信息。
RunMainForm(autoStart, hide, service);
}
//
XMLMessageManager.WriteExceptionToLog(e);
}
}
private
static
void
CreateMainForm(
bool
autoStart,
bool
hide,
bool
service)
{
if
(m_MainForm
!=
null
)
{
m_MainForm.Dispose();
m_MainForm
=
null
;
}
m_MainForm
=
new
MainForm(autoStart, hide, service);
Application.Run(m_MainForm);
}
}
}
查看全文
相关阅读:
SQL Server中的Merge关键字
详解公用表表达式(CTE)
SQL Server优化50法
Chrome下的脚本管理器
初步设计了一下视频工具合集的界面
迅雷的剪贴板冲突好强大
在C#中用MediaInfo获取视频或音频的属性
用Command模式简单的实现Undo&Redo功能
用DoddleReport快速生成报表
移动支付时代早日来临吧
原文地址:https://www.cnblogs.com/xinyuxin912/p/1331288.html
最新文章
详解游标
从外到内提高SQL Server数据库性能
行转列的思考
SQL Server中的执行引擎入门
SQL查询入门(上篇)
TSQL查询进阶变量
浅谈SQL Server 对于内存的管理
TSQL查询进阶基于列的逻辑表达式
利用SqlBulkCopy插入数据
[转]详细讲解提高数据库查询效率的实用方法、外键关于性能
热门文章
SqlBulkCopy加了事务真的会变快吗?
转]SQLServerDBA十大必备工具
临时表与表变量深入探究
SQL查询入门(中篇)
SQL养成一个好习惯是一笔财富
TSQL中的GROUP BY GROUPING SETS
效率最高的Excel数据导入续SSIS Package包制作图解全过程
对路径的访问被拒绝
理解SQL SERVER中的分区表
数据库范式那些事
Copyright © 2011-2022 走看看