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?

在.net中制作单实例应用程序的最佳方法是什么?

可能重复: 创建单个实例应用程序的正确方法是什么? 在.NET中阻止给定应用程序的多个实例? 我检查是否存在另一个同名的进程? (如果用户没有权限这么做?) 将文件写入磁盘并在退出之前将其删除? (异常终止怎么办?) 这将是一个最佳做法? 您可以使用互斥锁。 bool firstInstance = true; using (Mutex mutex = new Mutex(true, "MyApplicationName", out firstInstance)) { if (firstInstance) {

Start application just one time C#

This question already has an answer here: What is the correct way to create a single-instance application? 35 answers Please check this question: Run single instance of an application using Mutex You are looking for Mutex to check if you application is running. Then find your running application Process and bring it forward. The way I have achieved this is to use a Mutex . Each instanc

只启动一次应用程序C#

这个问题在这里已经有了答案: 创建单实例应用程序的正确方法是什么? 35个答案 请检查这个问题:使用Mutex运行应用程序的单个实例 您正在寻找Mutex来检查您的应用程序是否正在运行。 然后找到正在运行的应用Process并将其推向前进。 我实现这个的方式是使用Mutex 。 应用程序的每个实例都会在启动时尝试锁定互斥锁。 如果他们成功了,他们就会跑步; 如果他们失败了,他们不会。 无法运行的应用程序还会恢复并激

Check if other instances of a WPF application are/aren't running

This question already has an answer here: What is the correct way to create a single-instance application? 35 answers The get processes by name strategy can fail if the exe has been copied and renamed. Debugging can also be problematic because .vshost is appended to the process name. To create a single instance application in WPF, you can start by removing the StartupUri attribute from th

检查WPF应用程序的其他实例是否正在运行

这个问题在这里已经有了答案: 创建单实例应用程序的正确方法是什么? 35个答案 如果exe被复制并重命名,则按名称获取策略可能会失败。 调试也可能有问题,因为.vshost被附加到进程名称。 要在WPF中创建单个实例应用程序,您可以先从App.Xaml文件中删除StartupUri属性,使其看起来像这样... <Application x:Class="SingleInstance.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentati

How to make my app singleton application?

This question already has an answer here: What is the correct way to create a single-instance application? 35 answers Here are some good sample applications. Below is one possible way. public static Process RunningInstance() { Process current = Process.GetCurrentProcess(); Process[] processes = Process.GetProcessesByName (current.ProcessName); //Loop through the running pr

如何让我的应用程序单身应用程序?

这个问题在这里已经有了答案: 创建单实例应用程序的正确方法是什么? 35个答案 这里有一些好的示例应用程序。 以下是一种可能的方法。 public static Process RunningInstance() { Process current = Process.GetCurrentProcess(); Process[] processes = Process.GetProcessesByName (current.ProcessName); //Loop through the running processes in with the same name foreach (Process proces

How to check if a WPF application is already running?

Possible Duplicate: What is the correct way to create a single instance application? How can I check if my application is already open? If my application is already running, I want to show it instead of opening a new instance. [DllImport("user32.dll")] private static extern Boolean ShowWindow(IntPtr hWnd, Int32 nCmdShow); static void Main() { Process currentProcess = Process.GetCurren

如何检查一个WPF应用程序是否已经在运行?

可能重复: 创建单个实例应用程序的正确方法是什么? 我如何检查我的应用程序是否已经打开? 如果我的应用程序已经运行,我想显示它而不是打开一个新的实例。 [DllImport("user32.dll")] private static extern Boolean ShowWindow(IntPtr hWnd, Int32 nCmdShow); static void Main() { Process currentProcess = Process.GetCurrentProcess(); var runningProcess = (from process in Process.GetProcesses()

Ensuring only one application instance

Possible Duplicate: What is the correct way to create a single instance application? I have a Winforms app, which launches a splash screen via the following code: Hide(); bool done = false; // Below is a closure which will work with outer variables. ThreadPool.QueueUserWorkItem(x => { using (va

确保只有一个应用程序实例

可能重复: 创建单个实例应用程序的正确方法是什么? 我有一个Winforms应用程序,它通过以下代码启动启动屏幕: Hide(); bool done = false; // Below is a closure which will work with outer variables. ThreadPool.QueueUserWorkItem(x => { using (var splashForm = new SplashScreen())

How to force C# .net app to run only one instance in Windows?

Possible Duplicate: What is the correct way to create a single instance application? How to force C# .net app to run only one instance in Windows? I prefer a mutex solution similar to the following. As this way it re-focuses on the app if it is already loaded using System.Threading; [DllImport("user32.dll")] [return: MarshalAs(UnmanagedType.Bool)] static extern bool SetForegroundWindow(I

如何强制C#.net应用程序在Windows中只运行一个实例?

可能重复: 创建单个实例应用程序的正确方法是什么? 如何强制C#.net应用程序在Windows中只运行一个实例? 我更喜欢类似于以下的互斥解决方案。 正如这种方式,它重新集中在应用程序,如果它已经加载 using System.Threading; [DllImport("user32.dll")] [return: MarshalAs(UnmanagedType.Bool)] static extern bool SetForegroundWindow(IntPtr hWnd); /// <summary> /// The main entry point for the applic

Simpler Singleton Based on Jon Skeet's Singleton

I need a singleton in my code. I read Jon Skeet's page on Singletons and selected this model (#4) based on his recommendation: public sealed class Singleton { private static readonly Singleton instance = new Singleton(); // Explicit static constructor to tell C# compiler // not to mark type as beforefieldinit static Singleton(){} private Singleton(){} public stati

基于Jon Skeet的Singleton的更简单的Singleton

我需要一个单例代码。 我读了Jon Skeet关于Singletons的一页,并根据他的建议选择了这个模型(#4): public sealed class Singleton { private static readonly Singleton instance = new Singleton(); // Explicit static constructor to tell C# compiler // not to mark type as beforefieldinit static Singleton(){} private Singleton(){} public static Singleton Instance {

Singleton implementation laziness with static constructor

Jon Skeet suggests in his singleton implementation that if you require the maximum laziness for your singleton you should add a static constructor which will make the compiler mark the type as beforefieldinit. However, I did some testing and it seems that it's more lazy without the beforefieldinit. Code sample (the private constructor call outputs to console and verifies that the fields w

单例实现懒惰与静态构造函数

Jon Skeet在他的单例实现中建议,如果你需要单例的最大懒惰,你应该添加一个静态构造函数,它将使编译器将类型标记为beforefieldinit。 但是,我做了一些测试,似乎没有beforefieldinit就更加懒惰。 代码示例(私有构造函数调用输出到控制台并验证字段已初始化: public sealed class Singleton { private static readonly Singleton instance = new Singleton(); public static string Stub() { retur

Common Linq / Standard Query Operator mistakes/mis

对于不是来自功能编程背景的程序员,是否有错误需要避免? The biggest mistake people tend to make is to misunderstand the laziness and evaluation rules for a LINQ query: Queries are lazy: they are not executed until you iterate over them: // This does nothing! No query executed! var matches = results.Where(i => i.Foo == 42); // Iterating them will actually do the query. foreach (var match in

常见Linq /标准查询操作员错误/错误

对于不是来自功能编程背景的程序员,是否有错误需要避免? 人们倾向于犯的最大错误是误解了LINQ查询的懒惰和评估规则: 查询是懒惰的:它们在迭代它们之前不会被执行: // This does nothing! No query executed! var matches = results.Where(i => i.Foo == 42); // Iterating them will actually do the query. foreach (var match in matches) { ... } 此外,结果不会被缓存。 每次迭代它们时都会计算它们: var mat