什么是使.NET单实例应用程序的最佳方式? [英] What is the best way to make a single instance application in .net?
本文介绍了什么是使.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
}
}
这篇关于什么是使.NET单实例应用程序的最佳方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文