TinyMind
.net.cn
首页
资讯
文章
注册
登录
WPF 同一个程序 只允许 同时运行一个
XE2011
关注文章
TinyMind专栏频道
方法2
当程序已经运行了 再运行这个程序时,则显示当前这个窗体
http://code.3rbang.com/cshape-run-one/
VS2013附件:
http://files.cnblogs.com/xe2011/WpfApplication_System.Threading.Mutex.rar
.NET 已经提供一个 类
System
.
Threading
.
Mutex
所以不采用API
CreateMutex
在
App.xaml.cs
中实现代码,重这写
OnStartup
using
System
;
using
System
.
Collections
.
Generic
;
using
System
.
Configuration
;
using
System
.
Data
;
using
System
.
Windows
;
namespace
WpfApplication7
{
///
<summary>
///
App.xaml的交互逻辑
///
</summary>
public
partial
class
App
:
Application
{
#region
DllImport...
[
System
.
Runtime
.
InteropServices
.
DllImport
(
"User32.dll"
)]
private
static
extern
bool
SetForegroundWindow
(
IntPtr
hWnd
);
[
System
.
Runtime
.
InteropServices
.
DllImport
(
"User32.dll"
)]
private
static
extern
bool
ShowWindowAsync
(
IntPtr
hWnd
,
int
cmdShow
);
private
const
int
SW_SHOW
=1;
#endregion
bool
createdNew
;
protected
override
void
OnStartup
(
StartupEventArgs
e
)
{
System
.
Threading
.
Mutex
mutex
=
new
System
.
Threading
.
Mutex
(
true
,
"HelloRoman"
,
out
createdNew
);
if
(!
createdNew
)
{
System
.
Diagnostics
.
Process
progress1
=
GetExistProcess
();
if
(
progress1
!=
null
)
{
ShowMainWindow
(
progress1
);
Environment
.
Exit
(0);
}
}
}
///
<summary>
///
最前端显示主窗体
///
</summary>
///
<paramname="process"></param>
private
void
ShowMainWindow
(
System
.
Diagnostics
.
Process
process
)
{
IntPtr
mainWindowHandle1=
process
.
MainWindowHandle
;
if
(mainWindowHandle1!=
IntPtr
.
Zero
)
{
ShowWindowAsync
(mainWindowHandle1,
SW_SHOW
);
SetForegroundWindow
(mainWindowHandle1);
}
}
///
<summary>
///
查看程序是否已经运行
///
</summary>
///
<returns></returns>
private
static
System
.
Diagnostics
.
Process
GetExistProcess
()
{
System
.
Diagnostics
.
Process
currentProcess=
System
.
Diagnostics
.
Process
.
GetCurrentProcess
();
foreach
(
System
.
Diagnostics
.
Process
process1
in
System
.
Diagnostics
.
Process
.
GetProcessesByName
(currentProcess.
ProcessName
))
{
if
((process1.
Id
!=currentProcess.
Id
)&&
(
System
.
Reflection
.
Assembly
.
GetExecutingAssembly
().
Location
==currentProcess.
MainModule
.
FileName
))
{
return
process1;
}
}
return
null
;
}
}
}
方法1
using
System
;
using
System
.
Collections
.
Generic
;
using
System
.
Configuration
;
using
System
.
Data
;
using
System
.
Linq
;
using
System
.
Windows
;
namespace
WpfApplication6
{
///
<summary>
///
App.xaml的交互逻辑
///
</summary>
public
partial
class
App
:
Application
{
[
System
.
Runtime
.
InteropServices
.
DllImport
(
"kernel32.dll"
,
EntryPoint
=
"CreateMutex"
)]
public
static
extern
IntPtr
CreateMutex
(
int
lpSecurityAttributes
,
bool
bInitialOwner
,
string
lpName
);
[
System
.
Runtime
.
InteropServices
.
DllImport
(
"kernel32.dll"
,
EntryPoint
=
"GetLastError"
)]
public
static
extern
int
GetLastError
();
private
const
int
ERROR_ALREADY_EXISTS
=183;
protected
override
void
OnStartup
(
StartupEventArgs
e
)
{
string
appTitle
=
"2014年5月31日7:28:29"
;
IntPtr
hMutex
=
CreateMutex
(0,
true
,
appTitle
);
if
(
GetLastError
()==
ERROR_ALREADY_EXISTS
)
{
MessageBox
.
Show
(
"程序已经运行!"
);
Application
.
Current
.
Shutdown
();
}
}
}
}
来自为知笔记(Wiz)
优质内容筛选与推荐
>>
1、android之Intent小结
2、[LeetCode] String to Integer (atoi)
3、vue学习链接
4、VMware9虚拟机安装MAC OS X Mountain Lion 10.8.2详细图文教程
5、[转]MySQL的limit查询优化
赞赏
长按二维码向我转账
受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。
阅读
好看
已推荐到看一看
你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
取消
推荐
我知道了
已取消,“好看”想法已同步删除
知道了
已推荐到看一看
和朋友分享想法
最多200字,当前共
字
发送
已发送
朋友将在看一看看到
确定
分享你的想法...
取消
分享想法到看一看
确定
最多200字,当前共
字
发送中
网络异常,请稍后重试
知道了
微信扫一扫
关注该公众号
0
│
收藏
│
举报