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);
}
}
}
查看全文
相关阅读:
第一篇博客
margin 与 padding
CSS伪类
CSS定位
利用css布局在图片插入文字
CSS选择符
CSS伪类
CSS语法顺序
CSS样式特点及优先级
frame-框架
原文地址:https://www.cnblogs.com/xinyuxin912/p/1331288.html
最新文章
poj2299 树状数组入门题
Hibernate使用
Linux JDK 安装及卸载 http://www.cnblogs.com/benio/archive/2010/09/14/1825909.html
Zip文件压缩(加密||非加密||压缩指定目录||压缩目录下的单个文件||根据路径压缩||根据流压缩)
XML解析
Spring MVC
JSON http://www.cnblogs.com/haippy/archive/2012/05/20/2509329.html
根据word模板(contract_templet.tld)生成并下载word合同及根据wordHTML模板(contract_templetHTML.tld)预览合同内容
JS获取form表单所有属性值
QBC
热门文章
js打印 http://www.cnblogs.com/robertfang/articles/1599551.html
未命名
Java子类继承父类详解
线程池
王垠-完全用GNU/Linux工作
SQL连接(内连接、外连接(左连接、外连接、完整外连接)、交叉连接(cross join))
Java代码优化
同一mysql种复制数据库命令
Java序列化
Java内存分析
Copyright © 2011-2022 走看看