且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

应用程序的单个实例

更新时间:2023-11-22 23:03:04




它非常简单,多亏了.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" &LT; JB ***** @ topsys.fr&GT;在留言新闻中写道: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" &LT;无**** @ nodomain.com&GT; é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" &LT; JB ***** @ topsys.fr&GT; 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?