应用程序的单个实例 [英] Single instance of app
问题描述
我想确保我的应用程序中只有一个实例可以随时在
单台PC上运行。我明白我可以通过使用互斥量来实现这一点
,如果我不能取得互斥锁的所有权,我知道我的程序还有另一个
实例运行。
问题是,如何获得新的实例与旧
实例进行通信?新的将使用命令行参数
启动,我想将其传递给旧实例以供执行。在我的VB6时代,我使用DDE(旧的,但安全的)做了这个
。
什么是C#等价物?
你能否指点我可能需要合作的课程(没有
详细代码,请 - 我正在努力为自己学习这些东西)。
谢谢
史蒂夫
I want to ensure that there is only ever one instance of my app running on a
single PC at any time. I understand that I can achieve this by using a mutex
and, if I can''t take ownership of the mutex, I know there is another
instance of my program running.
Problem is, how do I get the "new" instance to communicate with the "old"
instance? The new one will have been started with a command line parameter
that I want to pass to the old instance for execution. In my VB6 days, I did
this using DDE (old, but safe).
What''s the C# equivalent?
Can you point me at the classes I might need to work with please (No
detailed code, please - I''m trying to learn this stuff for myself).
Thanks
Steve
推荐答案
它非常简单,多亏了.net框架提供了你需要的一切;-)
使用System.Diagnostics;
public static bool IsOneInstance()
{
流程pcur = Process.GetCurrentProcess();
流程[] ps =流程.GetProcesses();
foreach(ps中的处理p)
{
if(pcur.Id!= p.Id)
if(pcur.ProcessName == p.ProcessName)
返回false;
}
返回是的;
}
Jey
" Steve Barnett" <无**** @ nodomain.com> écritdansle message de news:%2 **************** @ TK2MSFTNGP12.phx.gbl ...
Hi,
it''s quite simple thanks to .net framework which provides everything you need ;-)
using System.Diagnostics;
public static bool IsOneInstance()
{
Process pcur = Process.GetCurrentProcess();
Process[] ps = Process.GetProcesses();
foreach( Process p in ps )
{
if ( pcur.Id != p.Id )
if ( pcur.ProcessName == p.ProcessName )
return false;
}
return true;
}
Jey
"Steve Barnett" <no****@nodomain.com> a écrit dans le message de news: %2****************@TK2MSFTNGP12.phx.gbl...
我想确保有只有我的应用程序的任何一个实例在任何时候在单个PC上运行。我知道我可以通过使用互斥锁来实现这一目标,如果我不能获取互斥锁的所有权,我知道我的程序还有另一个运行的实例。
实例?新的将使用命令行参数
启动,我想将其传递给旧实例以供执行。在我的VB6时代,我用DDE(旧的,但安全的)做了这个。
C#的等价物是什么?
你能指点我吗?我可能需要和你一起上课(没有
详细的代码,请 - 我正在努力为自己学习这些东西)。
谢谢
Steve
I want to ensure that there is only ever one instance of my app running on a
single PC at any time. I understand that I can achieve this by using a mutex
and, if I can''t take ownership of the mutex, I know there is another
instance of my program running.
Problem is, how do I get the "new" instance to communicate with the "old"
instance? The new one will have been started with a command line parameter
that I want to pass to the old instance for execution. In my VB6 days, I did
this using DDE (old, but safe).
What''s the C# equivalent?
Can you point me at the classes I might need to work with please (No
detailed code, please - I''m trying to learn this stuff for myself).
Thanks
Steve
谢谢
Steve
"Jér?me Bonnet" < JB ***** @ topsys.fr>在留言新闻中写道:43 *********************** @ news.wanadoo.fr ...
使用System.Diagnostics;
public static bool IsOneInstance()
{
流程pcur = Process.GetCurrentProcess();
流程[ ] ps = Process.GetProcesses();
foreach(ps中的过程p)
{
if(pcur.Id!= p。 Id)
if(pcur.ProcessName == p.ProcessName)
返回false;
}
返回true;
}
Jey
" Steve Barnett" <无**** @ nodomain.com> écritdansle message de news:%2 **************** @ TK2MSFTNGP12.phx.gbl ...
Thank you
Steve
"Jér?me Bonnet" <jb*****@topsys.fr> wrote in message news:43***********************@news.wanadoo.fr...
Hi,
it''s quite simple thanks to .net framework which provides everything you need ;-)
using System.Diagnostics;
public static bool IsOneInstance()
{
Process pcur = Process.GetCurrentProcess();
Process[] ps = Process.GetProcesses();
foreach( Process p in ps )
{
if ( pcur.Id != p.Id )
if ( pcur.ProcessName == p.ProcessName )
return false;
}
return true;
}
Jey
"Steve Barnett" <no****@nodomain.com> a écrit dans le message de news: %2****************@TK2MSFTNGP12.phx.gbl...
我想确保有只有我的应用程序的任何一个实例在任何时候在单个PC上运行。我知道我可以通过使用互斥锁来实现这一目标,如果我不能获取互斥锁的所有权,我知道我的程序还有另一个运行的实例。
实例?新的将使用命令行参数
启动,我想将其传递给旧实例以供执行。在我的VB6时代,我用DDE(旧的,但安全的)做了这个。
C#的等价物是什么?
你能指点我吗?我可能需要和你一起上课(没有
详细的代码,请 - 我正在努力为自己学习这些东西)。
谢谢
Steve
I want to ensure that there is only ever one instance of my app running on a
single PC at any time. I understand that I can achieve this by using a mutex
and, if I can''t take ownership of the mutex, I know there is another
instance of my program running.
Problem is, how do I get the "new" instance to communicate with the "old"
instance? The new one will have been started with a command line parameter
that I want to pass to the old instance for execution. In my VB6 days, I did
this using DDE (old, but safe).
What''s the C# equivalent?
Can you point me at the classes I might need to work with please (No
detailed code, please - I''m trying to learn this stuff for myself).
Thanks
Steve
"Jér?me Bonnet" < JB ***** @ topsys.fr> skrev i en meddelelse
news:43 *********************** @ news.wanadoo.fr ...
"Jér?me Bonnet" <jb*****@topsys.fr> skrev i en meddelelse
news:43***********************@news.wanadoo.fr...
使用System.Diagnostics;
公共静态bool IsOneInstance()
{进程pcur = Process.GetCurrentProcess();
进程[] ps = Process.GetProcesses ();
foreach(ps中的过程p)
{
if(pcur.Id!= p.Id)
if(pcur.ProcessName == p.ProcessName)
返回false;
}
返回true;
}
using System.Diagnostics;
public static bool IsOneInstance()
{
Process pcur = Process.GetCurrentProcess();
Process[] ps = Process.GetProcesses();
foreach( Process p in ps )
{
if ( pcur.Id != p.Id )
if ( pcur.ProcessName == p.ProcessName )
return false;
}
return true;
}
是否有可能两个你的流程几乎同时启动
,相互检测,从这种方法返回false - 并且
因此终止?
Would it be possible for two of your processes to start almost
simultaneously, detect each other, return false from this method - and
therefore both terminate?
这篇关于应用程序的单个实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!