正确.NET的方式来实现单实例应用程序 [英] Correct .NET way to implement a single instance application

查看:113
本文介绍了正确.NET的方式来实现单实例应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到的StackOverflow上至少有三种不同的方法来实现这一点。

I have seen at least three distinct methods on StackOverflow for achieving this.

  1. 使用互斥:<一href="http://stackoverflow.com/questions/19147/what-is-the-correct-way-to-create-a-single-instance-application">Accepted回答此等问题

使用Microsoft.VisualBasic程序库的WindowsFormsApplicationBase:<一href="http://stackoverflow.com/questions/19147/what-is-the-correct-way-to-create-a-single-instance-application/19326#19326">Second最高的投票回答此等问题

Using the Microsoft.VisualBasic library's WindowsFormsApplicationBase: Second highest voted answer to this SO question

使用Process.GetProcessNames检查,如果您的应用程序正在运行:在这里方法被张贴作为一个答案这 SO质疑

Using Process.GetProcessNames to check if your application is running: Method here was posted as an answer to this SO question

我敢肯定,有更多的方式来做到这一点。

I'm sure there are more ways to do this as well.

我只是想知道,如果其中一个是preferred和什么样的后果可能是,如果我选择了错误的。

I'm simply wondering if one of these is preferred and what the consequences might be if I pick the "wrong" one.

推荐答案

有疑问时,的总是的preFER是随机配备在.NET框架的实现。你可以有很高的期望,这样的实现是由成千上万的程序员的测试,进行了认真审查的安全性和可用性,并会维持几年。

When in doubt, always prefer an implementation that's included in the .NET framework. You can have high expectations that such an implementation is tested by hundreds of thousands of programmers, has been carefully reviewed for security and usability and will be maintained for years to come.

互斥锁的方法是一个容易走了。但是,由于它从pretty的严重的安全问题。 A 拒绝服务的攻击是很简单得走了,你不能让你的互斥一个秘密,任何人的名字可以平凡使用相同的名称和prevent创建一个互斥程序从以往开始了起来。

The mutex approach is an easy one to get going. It however suffers from a pretty severe security problem. A denial of service attack is very simple to get going, you cannot keep the name of your mutex a secret and anybody can trivially create a mutex with the same name and prevent your program from ever starting up.

进程名称的做法是有严重缺陷的出于同样的原因。谁也不能保证一个进程名是唯一的。不只是简单的利用,但是意外轻松地触发。

The process name approach is deeply flawed for the same reason. There is no guarantee that a process name is unique. Not just easy to exploit but easily triggered by accident.

WindowsFormsApplicationBase具有在C#程序员眼中的形象问题。他们呛的命名空间名称,并假定他们的计划会以某种方式被感染VB-主义。这是废话,这只是一个普通的.NET类是可用任何语言。

WindowsFormsApplicationBase has an image problem in the eyes of C# programmers. They choke at the namespace name and assume that their program will somehow be infected with vb-isms. That's nonsense, it is just a plain .NET class that's useable in any language.

这篇关于正确.NET的方式来实现单实例应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆