且构网

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

防止两次打开程序

更新时间:2022-12-19 21:17:28

25/04/2005 9.17.52

Tolga Tanriverdi< to *** @ sylveria.gen.tr&GT;写在消息中

< #M ************** @ TK2MSFTNGP15.phx.gbl>
25/04/2005 9.17.52
Tolga Tanriverdi <to***@sylveria.gen.tr> wrote in message
<#M**************@TK2MSFTNGP15.phx.gbl>
我做了一个带有c#的gui程序但是我想要即使人们双击
我的程序不止一次它只会打开一次
这样的东西可能会如何?
I made a gui program with c# but i want even if people double click my program more than once it would only open once
is something like that possible if it is how?




我已经用过这个课了,工作得很好..这是在vb.net,我希望这是

不是问题...
http://www.codeproject.com/vb/net/sing_inistan.asp


处理currentProcess = Process.GetCurrentProcess();

if(Process.GetProcessesByName(currentProcess.Proce ssName,

currentProcess.MachineName).Length> 1)

{

Console.Write(已经在运行);

控制台.Read();

}


Gabriel Lozano-Morán
Process currentProcess = Process.GetCurrentProcess();
if(Process.GetProcessesByName(currentProcess.Proce ssName,
currentProcess.MachineName).Length>1)
{
Console.Write("Already running");
Console.Read();
}

Gabriel Lozano-Morán




Tolga Tanriv ERDI&QUOT; &LT;到*** @ sylveria.gen.tr&GT;在消息中写道

新闻:%2 **************** @ TK2MSFTNGP15.phx.gbl ...

"Tolga Tanriverdi" <to***@sylveria.gen.tr> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
我做了一个带有c#的gui程序但是我想要即使人们多次双击我的
程序它也只会打开一次
这样的东西可能是这样的吗?
I made a gui program with c# but i want even if people double click my
program more than once it would only open once
is something like that possible if it is how?




使用全局互斥锁识别正在运行的实例。

这样可以防止在机器范围内运行多个实例。


bool freeToRun;

string safeName =" Global \\StringUniquelyIdentifyingThisApplication" ;

使用(System.Threading.Mutex m = new System.Threading.Mutex(true,safeName

,out freeToRun))

{

if(freeToRun)

Application.Run(new MainForm());

MessageBox.Show("已经在运行......) ;,safeName);

}


威利。



Use a global mutex to identify your running instance.
This way you prevent running multiple instances machine wide.

bool freeToRun;
string safeName = "Global\\StringUniquelyIdentifyingThisApplication" ;
using(System.Threading.Mutex m = new System.Threading.Mutex(true, safeName
, out freeToRun))
{
if (freeToRun)
Application.Run (new MainForm());
MessageBox.Show("Already running...", safeName);
}

Willy.