一个互斥谜题;单个应用程序实例 [英] A mutex puzzle; single instance of application

查看:92
本文介绍了一个互斥谜题;单个应用程序实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请参阅:
http://www.ai .uga.edu / mc / SingleInstance.html


尝试使用互斥锁时只允许我的应用程序的一个实例运行

一次(C#Programmer's Cookbook中的食谱4.12),我发现如果

互斥锁在Main()中的局部变量中,我的程序启动任何窗口
$在Application.Run()之前的b $ b,它会在执行此操作时丢失互斥锁。就我所知,它不应该是
。将互斥变量移到Main()

之外并使其静态消除了这个问题。


有什么想法吗?是否存在可变范围和程度的微妙问题,我无法理解?在启动

窗口时,进程是否会丢失其互斥锁?为什么?


(互斥变量仍然存在,m.Handle具有相同的值;但是根据ProcessExplorer,
,互斥锁不再存在。所有这些

仅适用于从Windows启动的进程,而不是在IDE下!)


-


Michael A. Covington - 人工智能中心 - 佐治亚大学


在核心C#语言中,根本不可能有一个未初始化的

变量,一个''悬空''指针,或一个索引数组的表达式

超出其范围。因此,消除了常规困扰C和C ++
程序的所有类别的错误。 - A. Hejlsberg,C#编程语言

See:
http://www.ai.uga.edu/mc/SingleInstance.html

While attempting to use a mutex to allow only one instance of my app to run
at a time (Recipe 4.12 in C# Programmer''s Cookbook), I found that if the
mutex is in a local variable in Main(), and my program launches any windows
before Application.Run(), it will lose the mutex upon doing so. It
shouldn''t, as far as I can see. Moving the mutex variable outside Main()
and making it static eliminates the problem.

Any ideas? Is there a subtle matter of variable scope and extent that I
haven''t understood? Does a process lose its mutexes when it launches a
window? Why?

(The mutex variable still exists, and m.Handle has the same value; but
according to ProcessExplorer, the mutex no longer exists. And all of this
happens only for processes launched from Windows, not under the IDE!)

--

Michael A. Covington - Artificial Intelligence Ctr - University of Georgia

"In the core C# language it is simply not possible to have an uninitialized
variable, a ''dangling'' pointer, or an expression that indexes an array
beyond its bounds. Whole categories of bugs that routinely plague C and C++
programs are thus eliminated." - A. Hejlsberg, The C# Programming Language

推荐答案

Michael A. Covington< lo ** @ www.covingtoninnovations.com.for.address>

写道:
Michael A. Covington <lo**@www.covingtoninnovations.com.for.address>
wrote:
参见:
http://www.ai.uga.edu/mc/SingleInstance.html

尝试使用互斥锁只允许我的一个实例应用程序一次运行
(C#Programmer's Cookbook中的食谱4.12),我发现如果
互斥锁在Main()中的局部变量中,我的程序启动任何窗口
任何想法?是否有一个我可能无法理解的范围和范围可变的微妙问题?一个进程在启动窗口时是否会丢失其互斥锁?为什么?

(互斥变量仍然存在,并且m.Handle具有相同的值;但是根据ProcessExplorer,互斥体不再存在。并且所有这些都发生了仅适用于从Windows启动的进程,而不是在IDE下启动!)
See:
http://www.ai.uga.edu/mc/SingleInstance.html

While attempting to use a mutex to allow only one instance of my app to run
at a time (Recipe 4.12 in C# Programmer''s Cookbook), I found that if the
mutex is in a local variable in Main(), and my program launches any windows
before Application.Run(), it will lose the mutex upon doing so. It
shouldn''t, as far as I can see. Moving the mutex variable outside Main()
and making it static eliminates the problem.

Any ideas? Is there a subtle matter of variable scope and extent that I
haven''t understood? Does a process lose its mutexes when it launches a
window? Why?

(The mutex variable still exists, and m.Handle has the same value; but
according to ProcessExplorer, the mutex no longer exists. And all of this
happens only for processes launched from Windows, not under the IDE!)




我的猜测是JIT可以注意到Mutex不再是
引用,因此将其标记为可用于垃圾回收。作为测试这个的方式,请输入:


GC.KeepAlive(myMutex);


在Main方法的* end *。


如果有效,请告诉我,我会在FAQ中提及它。


-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet

如果回复小组,请不要给我发邮件



My guess is that the JIT can notice that the Mutex is no longer
referenced, and thus marks it as available for garbage collection. As a
way of testing this, put the line:

GC.KeepAlive (myMutex);

at the *end* of the Main method.

Let me know if that works, and I''ll mention it in the FAQ.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


适用于我,你能发布失败的Main()方法吗?


Willy。


Michael A. Covington <螺** @ www.covingtoninnovations.com.for.address>在消息新闻中写了

:OP ************** @ TK2MSFTNGP11.phx.gbl ...
Works for me, could you post your failing Main() method?

Willy.

"Michael A. Covington" <lo**@www.covingtoninnovations.com.for.address> wrote
in message news:OP**************@TK2MSFTNGP11.phx.gbl...
参见:
http://www.ai.uga.edu/mc/SingleInstance .html

尝试使用互斥锁只允许我的应用程序的一个实例一次运行
(C#Programmer'中的配方4.12) Cookbook),我发现如果
互斥锁在Main()中的局部变量中,并且我的程序在Application.Run()之前启动任何窗口
,它将失去互斥锁这样做。就我所见,它不应该。将互斥变量移到Main()外面并使其静态消除了这个问题。

任何想法?是否有一个我可能无法理解的范围和范围可变的微妙问题?一个进程在启动窗口时是否会丢失其互斥锁?为什么?

(互斥变量仍然存在,并且m.Handle具有相同的值;但是根据ProcessExplorer,互斥体不再存在。并且所有这些都发生了仅适用于从Windows启动的流程,而不适用于IDE!)

-

Michael A. Covington - 人工智能中心 - 佐治亚大学
在核心C#语言中,根本不可能有一个
未初始化的变量,一个''悬空''指针,或一个将数组索引超出其范围的表达式。因此消除了常规困扰C和C ++程序的所有类别的错误。 - A. Hejlsberg,C#编程
语言
See:
http://www.ai.uga.edu/mc/SingleInstance.html

While attempting to use a mutex to allow only one instance of my app to
run
at a time (Recipe 4.12 in C# Programmer''s Cookbook), I found that if the
mutex is in a local variable in Main(), and my program launches any
windows
before Application.Run(), it will lose the mutex upon doing so. It
shouldn''t, as far as I can see. Moving the mutex variable outside Main()
and making it static eliminates the problem.

Any ideas? Is there a subtle matter of variable scope and extent that I
haven''t understood? Does a process lose its mutexes when it launches a
window? Why?

(The mutex variable still exists, and m.Handle has the same value; but
according to ProcessExplorer, the mutex no longer exists. And all of this
happens only for processes launched from Windows, not under the IDE!)

--

Michael A. Covington - Artificial Intelligence Ctr - University of Georgia

"In the core C# language it is simply not possible to have an
uninitialized
variable, a ''dangling'' pointer, or an expression that indexes an array
beyond its bounds. Whole categories of bugs that routinely plague C and
C++
programs are thus eliminated." - A. Hejlsberg, The C# Programming
Language





" Jon Skeet [C# MVP] QUOT; < SK *** @ pobox.com>在消息中写道

新闻:MP ************************ @ msnews.microsoft.c om ...

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Michael A. Covington< lo ** @ www.covingtoninnovations.com.for.address>
写道:
Michael A. Covington <lo**@www.covingtoninnovations.com.for.address>
wrote:
参见:
http://www.ai.uga.edu/mc/SingleInstance.html

尝试使用互斥锁只允许我的应用程序的一个实例一次运行
(C#Programmer's Cookbook中的食谱4.12),我发现如果
mutex在Main()中的局部变量中,我的程序在Application.Run()之前启动任何
窗口,这样做会丢失互斥锁。就我所见,它不应该。将互斥变量移到
Main()之外并使其静态消除了问题。

任何想法?是否有一个我可能无法理解的范围和范围可变的微妙问题?一个进程在启动窗口时是否会丢失其互斥锁?为什么?

(互斥变量仍然存在,并且m.Handle具有相同的值;但是根据ProcessExplorer,互斥锁不再存在。并且所有
都会发生这种情况仅适用于从Windows启动的进程,而不是在IDE下启动!)
See:
http://www.ai.uga.edu/mc/SingleInstance.html

While attempting to use a mutex to allow only one instance of my app to run at a time (Recipe 4.12 in C# Programmer''s Cookbook), I found that if the
mutex is in a local variable in Main(), and my program launches any windows before Application.Run(), it will lose the mutex upon doing so. It
shouldn''t, as far as I can see. Moving the mutex variable outside Main() and making it static eliminates the problem.

Any ideas? Is there a subtle matter of variable scope and extent that I
haven''t understood? Does a process lose its mutexes when it launches a
window? Why?

(The mutex variable still exists, and m.Handle has the same value; but
according to ProcessExplorer, the mutex no longer exists. And all of this happens only for processes launched from Windows, not under the IDE!)



我的猜测是,JIT可以注意到Mutex不再被引用,因此将其标记为可用于垃圾收集。作为测试方法的一种方式,请在主方法的* end *处放置GC:.KeepAlive(myMutex);

。让我知道如果有效,我会在常见问题解答中提及。



My guess is that the JIT can notice that the Mutex is no longer
referenced, and thus marks it as available for garbage collection. As a
way of testing this, put the line:

GC.KeepAlive (myMutex);

at the *end* of the Main method.

Let me know if that works, and I''ll mention it in the FAQ.




我会试试。请注意,稍后在Main()方法中插入对互斥锁

变量

(例如,MessageBox(m.Handle.ToString())的其他引用不会

帮助。如果GC认为我不再使用互斥变量,那就错了。

错误。


In无论如何我都会调查。谢谢!



I''ll try that. Note that inserting additional references to the mutex
variable
(e.g., MessageBox(m.Handle.ToString()) later on in the Main() method did not
help. If the GC thinks I''m no longer using the mutex variable, it''s
mistaken.

In any case I''ll investigate. Thanks!


这篇关于一个互斥谜题;单个应用程序实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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