互斥锁没有按预期工作 [英] Mutex not working as expected

查看:55
本文介绍了互斥锁没有按预期工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要确定我的应用程序只能在一台机器上运行一次,并且我使用Mutex实现了一个解决方案
。该解决方案已多次发布,但

我找到的一个很棒的链接是
http://www.yoda.arachsys.com/csharp/...ation.instance


问题是,当我切换用户(XP Professional)时,第二个用户可以打开应用程序的第二个实例
!如何防止这种情况?


这是我的代码的相关部分:


Sub Main()

Dim vBool As Boolean

Dim FirstInstance As New Mutex(False,UniqueMutexName",vBool)

如果不是vBool那么

将消息发布给用户并退出。

结束如果

解决方案

此代码只允许你拥有1个实例你的申请

运行


如果

(UBound(Diagnostics.Process.GetProcessesByName)(Dia gnostics.Process.GetCurrentProcess .ProcessName))

0)然后
MsgBox(不能启动thisgram的多个副本,

MsgBoxStyle.Exclamation)

Me.Close()

结束如果


" mfdatsw1" < MF ****** @ discussions.microsoft.com>在消息中写道

新闻:是********************************** @ microsof t.com ...我需要确定我的应用程序只能在一台机器上运行一次,并且我使用Mutex实现了一个解决方案。解决方案已经发布了很多次,
但是我找到了一个很棒的链接
http://www.yoda.arachsys.com/csharp/...ation.instance

问题是,当我切换用户(XP Professional)时,第二个用户可以打开应用程序的第二个实例!我该如何防止这种情况?

这里是我的代码的相关部分:

Sub Main()
Dim vBool As Boolean
Dim FirstInstance As New Mutex(False,UniqueMutexName,vBool)
如果不是vBool那么
将消息发布给用户并退出。
结束如果



Chris,

这段代码只允许你有1个你的应用程序运行

如果
(UBound(Diagnostics.Process.GetProcessesByName( Diagnostics.Process.GetCurrentProcess.ProcessName))

0)然后


MsgBox(无法启动此文件的多个副本,
MsgBoxStyle.Exclamation )
Me.Close()




如果有多个应用程序具有相同的

进程名称,则此代码将失败。流程名称不一定是unque。


-

MS Herfried K. Wagner

MVP< URL:http:/ /dotnet.mvps.org/>

VB< URL:http://dotnet.mvps.org/dotnet/faqs/>


这是正确的,如果有超过1个名为

的进程相同,代码将失败。

您是否见过一个已经过程的进程命名相同。我没有。

如果您知道更好的方法,请告诉我。


代码的重点是不允许相同的过程名称即将开始。

所以代码没有失败

它的工作方式是如何设计的


修复它的方法如果你正在寻找一个流程名称



(UBound(Diagnostics.Process.GetProcessesByName(你的流程名称))


>
Chris

" Herfried K. Wagner [MVP]"< hi *************** @ gmx.at>写道消息

news:e6 ************** @ TK2MSFTNGP12.phx.gbl ...

Chris,

Chris Calzaretta< cc ********* @ hotmail.com> schrieb:

此代码只允许您拥有1个应用程序实例
如果
(UBound(Diagnostics.Process.GetProcessesByName(Dia gnostics.Process.GetCurrentProcess.ProcessName))

> 0)然后


MsgBox(不能启动thisgram的多个副本,
MsgBoxStyle.Exclamation)
Me.Close()



如果有多个具有相同
进程名称的应用程序,则此代码将失败。流程名称不一定是unque。

- MS Herfried K. Wagner
MVP< URL:http://dotnet.mvps.org/>
VB< URL:http://dotnet.mvps.org/dotnet/faqs/>



I need to be certain my application only runs once on a machine, and I
implemented a solution using Mutex. The solution was posted many times, but
one great link I found is
http://www.yoda.arachsys.com/csharp/...ation.instance

The problem is, when I switch users (XP Professional) the second user can
open a second instance of the application! How can I prevent this?

Here''s the relevant part of my code:

Sub Main()
Dim vBool As Boolean
Dim FirstInstance As New Mutex(False, "UniqueMutexName", vBool)
If Not vBool Then
Post message to user and exit.
End If

解决方案

This code will only allow you to have 1 instqance of your appliccation
running

If
(UBound(Diagnostics.Process.GetProcessesByName(Dia gnostics.Process.GetCurrentProcess.ProcessName))

0) Then MsgBox("Cannot start more than one copy of thisgram",
MsgBoxStyle.Exclamation)
Me.Close()
End If

"mfdatsw1" <mf******@discussions.microsoft.com> wrote in message
news:BE**********************************@microsof t.com...I need to be certain my application only runs once on a machine, and I
implemented a solution using Mutex. The solution was posted many times,
but
one great link I found is
http://www.yoda.arachsys.com/csharp/...ation.instance

The problem is, when I switch users (XP Professional) the second user can
open a second instance of the application! How can I prevent this?

Here''s the relevant part of my code:

Sub Main()
Dim vBool As Boolean
Dim FirstInstance As New Mutex(False, "UniqueMutexName", vBool)
If Not vBool Then
Post message to user and exit.
End If



Chris,

"Chris Calzaretta" <cc*********@hotmail.com> schrieb:

This code will only allow you to have 1 instqance of your appliccation
running

If
(UBound(Diagnostics.Process.GetProcessesByName(Dia gnostics.Process.GetCurrentProcess.ProcessName))

0) Then


MsgBox("Cannot start more than one copy of thisgram",
MsgBoxStyle.Exclamation)
Me.Close()



This code will fail if there is more than one application with the same
process name. Process names are not necessarily unque.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>


that is correct the code will fail if there is more then 1 process named the
same.
Have you even seen a process that has been named the same. I have not.
If you know a better way of doing this then let me know.

the point of the code is to not allow the same proccess name to be started.
So the code is not failing
It is working how it was designed

way to fix it if your looking for a process name
is
(UBound(Diagnostics.Process.GetProcessesByName(YOU R PROCESS NAME))


Chris
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:e6**************@TK2MSFTNGP12.phx.gbl...

Chris,

"Chris Calzaretta" <cc*********@hotmail.com> schrieb:

This code will only allow you to have 1 instqance of your appliccation
running

If
(UBound(Diagnostics.Process.GetProcessesByName(Dia gnostics.Process.GetCurrentProcess.ProcessName))

> 0) Then


MsgBox("Cannot start more than one copy of thisgram",
MsgBoxStyle.Exclamation)
Me.Close()



This code will fail if there is more than one application with the same
process name. Process names are not necessarily unque.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>



这篇关于互斥锁没有按预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆