[英]C# Application Exit with Mutex
希望您能提供帮助,我一直在阅读有关互斥锁的信息,并且我相信我理解它 - 它应该只限于一个应用程序进程。
在这个社区的一些指导和一些谷歌搜索下——我已经创建了这个简单的代码。
关于应用程序:C# 中的简单 Windows 窗体应用程序
声明的变量:
static Mutex mutex = new Mutex(true, "TestAppForm");
在 AppForm_Load() 我有以下代码。
if (!mutex.WaitOne(2000))
{
System.Windows.Forms.Application.Exit();
return;
}
else
{
try
{
// My Code
}
finally
{
mutex.ReleaseMutex();
}
}
我相信我的代码有效,因为它只限于一个应用程序。
但是,我注意到当它打开应用程序然后通过模式退出代码将其关闭时,我可以看到它闪烁。
我想要达到的目标::
我想运行该应用程序并检查它是否已经在运行 - 如果没有,那么很好,然后继续其余的。 如果应用程序正在运行,我想结束进程然后我想在正在运行的应用程序上设置 FOCUS。
谢谢你。
==
感谢您在谷歌搜索后的所有帮助,我发现了这篇文章:
http://sanity-free.org/143/csharp_dotnet_single_instance_application.html
这帮助我让它工作。
AppForm_Load 不是应用程序的第一个入口点。 找到创建表单的代码并在那里执行。
不要检查AppForm_Load
中的互斥锁,而是在Main
方法中检查它,如下所示:
private static Mutex _mutex = new Mutex(true, "AppMutex");
static void Main()
{
if(!_mutex.WaitOne(0, true))
{
return;
}
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new AppForm());
}
如果你想创建一个单例应用程序,你不能在AppForm_Load
中实现Mutex
逻辑,因为当你到达那个点(这不是你的程序集的入口点/主要方法)时,这意味着你的应用程序已经启动了。 通过您的实现,一旦您注意到另一个先前创建的实例已经在运行,您最多可以关闭新实例......为什么不直接“阻止”创建新实例呢?
这是我的单例应用程序模板( Program
类是包含我的应用程序入口点static void Main
的静态类):
#region Using Directives
using System;
using System.Diagnostics;
using System.Globalization;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;
#endregion
namespace MyNamespace
{
public static class Program
{
#region Members: Static
private static Int32 s_MutexMessage;
private static Mutex s_Mutex;
#endregion
#region Properties: Static
public static Int32 MutexMessage
{
get { return s_MutexMessage; }
}
#endregion
#region Methods: Entry Point
[STAThread]
public static void Main()
{
Assembly assembly = Assembly.GetExecutingAssembly();
String assemblyGuid = ((GuidAttribute)assembly.GetCustomAttributes(typeof(GuidAttribute), true)[0]).Value;
String mutexName = String.Format(CultureInfo.InvariantCulture, "Local\\{{{0}}}", assemblyGuid);
s_MutexMessage = NativeMethods.RegisterWindowMessage(assemblyGuid);
Boolean mutexCreated;
s_Mutex = new Mutex(true, mutexName, out mutexCreated);
if (!mutexCreated)
{
NativeMethods.PostMessage((new IntPtr(0xFFFF)), s_MutexMessage, IntPtr.Zero, IntPtr.Zero);
return;
}
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new ApplicationForm());
s_Mutex.ReleaseMutex();
}
#endregion
}
}
然后,在表单类中(在我的示例中是ApplicationForm
):
protected override void WndProc(ref Message m)
{
if (m.Msg == Program.MutexMessage)
{
if (NativeMethods.IsIconic(Handle))
NativeMethods.ShowWindow(Handle, 0x00000009);
NativeMethods.SetForegroundWindow(Handle);
}
base.WndProc(ref m);
}
最后,为了完整起见,这里是我的代码使用的导入,位于NativeMethods
类中(记得将其标记为internal
,这是一个不容忽视的好习惯):
internal static class NativeMethods
{
#region Importations
[DllImport("User32.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Unicode, ExactSpelling = true, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern Boolean IsIconic([In] IntPtr windowHandle);
[DllImport("User32.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Unicode, ExactSpelling = false, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern Boolean PostMessage([In, Optional] IntPtr windowHandle, [In] Int32 message, [In] IntPtr wParameter, [In] IntPtr lParameter);
[DllImport("User32.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Unicode, ExactSpelling = true, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern Boolean SetForegroundWindow([In] IntPtr windowHandle);
[DllImport("User32.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Unicode, ExactSpelling = true, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern Boolean ShowWindow([In] IntPtr windowHandle, [In] Int32 command);
[DllImport("User32.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Unicode, ExactSpelling = false, SetLastError = true)]
internal static extern Int32 RegisterWindowMessage([In] String message);
}
此实现比仅基于Mutex
对象的传统实现要长一点,需要广泛使用本机互操作,并且必须在项目中的不同类之间拆分......但我很久以前就使用它模板,我可以肯定它是防弹的。
一个对象在此方法中被定义为数据,我们可以在 CSharp 编程类中使用常量数据类型更新单个数组中的多个变量,在 mutux 类中分配给该应用程序这是如何发生的,但可靠且可预测
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.