实行单在控制台应用程序C#的最佳方式吗? [英] Best way to implement singleton in a console application C#?

查看:133
本文介绍了实行单在控制台应用程序C#的最佳方式吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个控制台应用程序是基于服务器的。我只想要1个实例,在一次为一个特定的服务器上运行(这是无论谁可能运行它的用户)。

I have a console application that is server based. I only want 1 instance of it running at once for a particular server (this is regardless of the user who might be running it).

我需要添加一个检查,以它正在运行的肯定只有一个实例,我可以通过检查服务器上正在运行的进程已经做到这一点,但是这是最好的做法?

I need to add a check to make sure only 1 instance of it is running, I can already do this by checking the running processes on the server, but is this best practice?

由于我不断地寻找各种方法来提高编码风格和保持最新,有没有更好的方式来最近做到这一点?如果你想 - 如果不破不解决它,也许你是对的,但我想利用内置功能架构更有优势

Since I am constantly looking for ways to improve coding styles and stay up to date, is there a better way to do this lately? If you're thinking - "If it ain't broke don't fix it", Maybe you're right, but I want to take more advantage of framework built in functionality.

我使用的.NET版本3.5,这是一个控制台应用程序。

I am using .net v3.5, and this is a console application.

在此先感谢

推荐答案

您应该使用Mutex类,喜欢这里解释:的 C#.NET单实例应用程序

You should use Mutex class, like explained here: C# .NET Single Instance Application

这篇关于实行单在控制台应用程序C#的最佳方式吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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