会员
周边
新闻
博问
融资
云市场
所有博客
当前博客
我的博客
我的园子
账号设置
简洁模式
...
退出登录
注册
登录
netzlj
一个新的起点
博客园
首页
新随笔
联系
订阅
管理
windows mobile 实现单实例启动
Code
通过创建互斥量来实现但启动,下面贴部分代码:
IntPtr hMutex
=
IntPtr.Zero;
hMutex
=
CreateMutex(
null
,
true
,
"
SUPERMAIL-WM
"
);
int
err
=
Marshal.GetLastWin32Error();
if
(err
!=
0
)
{
//
如果已经有程序运行,则立刻退出
if
(err
==
ERROR_ALREADY_EXISTS)
{
return
;
}
//
如果没有,则重试三次
for
(
int
i
=
0
;i
<
3
;i
++
)
{
hMutex
=
CreateMutex(
null
,
true
,
"
SUPERMAIL-WM
"
);
err
=
Marshal.GetLastWin32Error();
if
(err
==
ERROR_ALREADY_EXISTS)
{
return
;
}
System.Threading.Thread.Sleep(
1000
);
}
}
下面是用到的win32 函数,通过P
/
Invoke调用
[DllImport(
"
coredll.Dll
"
, SetLastError
=
true
)]
private
static
extern
int
ReleaseMutex(IntPtr hMutex);
[DllImport(
"
coredll.Dll
"
, SetLastError
=
true
)]
private
static
extern
IntPtr CreateMutex(SECURITY_ATTRIBUTES lpMutexAttributes,
bool
bInitialOwner,
string
lpName);
[StructLayout(LayoutKind.Sequential)]
public
class
SECURITY_ATTRIBUTES
{
public
int
nLength;
public
int
lpSecurityDescriptor;
public
int
bInheritHandle;
}
const
int
ERROR_ALREADY_EXISTS
=
0183
;
记得在程序退出的时候要调用ReleaseMutex 来销毁互斥量。
posted @
2008-12-01 09:51
Camel
阅读(
301
) 评论(
0
)
编辑
收藏
举报
会员力量,点亮园子希望
刷新页面
返回顶部
公告