我想检查当用户双击应用程序图标时是否没有该应用程序的另一个实例正在运行。
我读过有关 My.Application 的内容,但我仍然不知道该怎么做。
这是我用过的东西...(.NET 2.0 上的 C#)
[STAThread]
private static void Main(string[] args)
{
//this follows best practices on
//ensuring that this is a single instance app.
string mutexName = "e50cf829-f6b9-471e-8d9f-67eac3699f09";
bool grantedOwnership;
//we prefix the mutexName with "Local\\" to allow this to run under terminal services.
//The "Local\\" prefix forces this into local user space.
//If we want to forbid this in TS, use the "Global\\" prefix.
Mutex singleInstanceMutex = new Mutex(true, "Global\\" + mutexName, out grantedOwnership);
try
{
if (!grantedOwnership)
{
MessageBox.Show("Error: X is already running.\n\nYou can only run one copy of X at a time.", "X", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
Application.Exit();
}
else
{
Application.Run(new X(args));
}
}
finally
{
singleInstanceMutex.Close();
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)