BDe*_*per 7 vb.net single-instance
我想检查用户双击应用程序图标的时间,该应用程序的另一个实例尚未运行.
我读到了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();
}
}
Run Code Online (Sandbox Code Playgroud)
在VB .NET中,有一个IsSingleInstance布尔属性可以为您完成工作.
在VB中(取自此处):
Public Class Program
Inherits Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase
Public Sub New()
Me.IsSingleInstance = True
End Sub
End Class
Run Code Online (Sandbox Code Playgroud)
以下是您在C#中使用它的方法(取自此处):
// SingleInstanceApplication.cs
class SingleInstanceApplication : WindowsFormsApplicationBase {
// Must call base constructor to ensure correct initial
// WindowsFormsApplicationBase configuration
public SingleInstanceApplication() {
// This ensures the underlying single-SDI framework is employed,
// and OnStartupNextInstance is fired
this.IsSingleInstance = true;
}
}
// Program.cs
static class Program {
[STAThread]
static void Main(string[] args) {
Application.EnableVisualStyles();
SingleInstanceApplication application =
new SingleInstanceApplication();
application.Run(args);
}
}
Run Code Online (Sandbox Code Playgroud)
确保在项目中引用Microsoft.VisualBasic.dll.
| 归档时间: |
|
| 查看次数: |
16377 次 |
| 最近记录: |