What is the best way to make a single instance application in .net?
Possible Duplicates:
What is the correct way to create a single instance application?
Prevent multiple instances of a given app in .NET?
Do I check whether another process with the same name exists? (What if the user doesn't have permission to do that?)
Write a file to disk and delete it before exiting? (what about abnormal termination?)
What would be a best practice to do this?
您可以使用互斥锁。
bool firstInstance = true;
using (Mutex mutex = new Mutex(true, "MyApplicationName", out firstInstance))
{
if (firstInstance)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm());
}
else
{
// Another instance loaded
}
}
您可以使用Process
类的GetProcessesByName
方法查找另一个具有相同名称的正在运行的进程,如果找到该进程,则退出。
Generally, a named mutex works best. Here's a link to en example: http://www.iridescence.no/post/CreatingaSingleInstanceApplicationinC.aspx
链接地址: http://www.djcxy.com/p/51146.html上一篇: 如何防止多次打开我的应用程序?