C# WPF 单实例最佳实践
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14506406/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF Single Instance Best Practices
提问by Tommaso Belluzzo
This is the code I implemented so far to create a single instance WPF application:
这是我迄今为止实现的用于创建单实例 WPF 应用程序的代码:
#region Using Directives
using System;
using System.Globalization;
using System.Reflection;
using System.Threading;
using System.Windows;
using System.Windows.Interop;
#endregion
namespace MyWPF
{
public partial class MainApplication : Application, IDisposable
{
#region Members
private Int32 m_Message;
private Mutex m_Mutex;
#endregion
#region Methods: Functions
private IntPtr HandleMessages(IntPtr handle, Int32 message, IntPtr wParameter, IntPtr lParameter, ref Boolean handled)
{
if (message == m_Message)
{
if (MainWindow.WindowState == WindowState.Minimized)
MainWindow.WindowState = WindowState.Normal;
Boolean topmost = MainWindow.Topmost;
MainWindow.Topmost = true;
MainWindow.Topmost = topmost;
}
return IntPtr.Zero;
}
private void Dispose(Boolean disposing)
{
if (disposing && (m_Mutex != null))
{
m_Mutex.ReleaseMutex();
m_Mutex.Close();
m_Mutex = null;
}
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
#endregion
#region Methods: Overrides
protected override void OnStartup(StartupEventArgs e)
{
Assembly assembly = Assembly.GetExecutingAssembly();
Boolean mutexCreated;
String mutexName = String.Format(CultureInfo.InvariantCulture, "Local\{{{0}}}{{{1}}}", assembly.GetType().GUID, assembly.GetName().Name);
m_Mutex = new Mutex(true, mutexName, out mutexCreated);
m_Message = NativeMethods.RegisterWindowMessage(mutexName);
if (!mutexCreated)
{
m_Mutex = null;
NativeMethods.PostMessage(NativeMethods.HWND_BROADCAST, m_Message, IntPtr.Zero, IntPtr.Zero);
Current.Shutdown();
return;
}
base.OnStartup(e);
MainWindow window = new MainWindow();
MainWindow = window;
window.Show();
HwndSource.FromHwnd((new WindowInteropHelper(window)).Handle).AddHook(new HwndSourceHook(HandleMessages));
}
protected override void OnExit(ExitEventArgs e)
{
Dispose();
base.OnExit(e);
}
#endregion
}
}
Everything works perfectly... but I have some doubts about it and I would like to receive your suggestions about how my approach could be improved.
一切都很完美……但我对此有一些疑问,我希望收到您关于如何改进我的方法的建议。
1) I was asked by Code Analysis to implement IDisposable
interface because I was using IDisposable
members (the Mutex
). Is my Dispose()
implementation good enough? Should I avoid it because it's never going to be called?
1) 代码分析要求我实现IDisposable
接口,因为我使用的是IDisposable
成员 (the Mutex
)。我的Dispose()
实现是否足够好?我应该避免它,因为它永远不会被调用吗?
2) It's better to use m_Mutex = new Mutex(true, mutexName, out mutexCreated);
and check for the result or to use m_Mutex = new Mutex(false, mutexName);
and then check for m_Mutex.WaitOne(TimeSpan.Zero, false);
? In case of multithreading I mean...
2)最好使用m_Mutex = new Mutex(true, mutexName, out mutexCreated);
并检查结果或使用m_Mutex = new Mutex(false, mutexName);
然后检查m_Mutex.WaitOne(TimeSpan.Zero, false);
?在多线程的情况下,我的意思是......
3) RegisterWindowMessage
API call should return UInt32
... but HwndSourceHook
is only accepting Int32
as message value... should I be worried about unexpected behaviors (like a result bigger than Int32.MaxValue
)?
3) RegisterWindowMessage
API 调用应该返回UInt32
......但HwndSourceHook
只接受Int32
作为消息值......我是否应该担心意外行为(例如大于 的结果Int32.MaxValue
)?
4) In OnStartup
override... should I execute base.OnStartup(e);
even if another instance is already running and I'm going to shutdown the application?
4) 在OnStartup
覆盖中...base.OnStartup(e);
即使另一个实例已经在运行并且我要关闭应用程序,我是否应该执行?
5) Is there a better way to bring the existing instance to the top that doesn't need to set Topmost
value? Maybe Activate()
?
5)有没有更好的方法可以将现有实例带到不需要设置Topmost
值的顶部?也许Activate()
?
6) Can you see any flaw in my approach? Something concerning multithreading, bad exceptions handling and something like that? For example... what happens if my application crashes between OnStartup
and OnExit
?
6) 你能看出我的方法有什么缺陷吗?一些关于多线程、错误的异常处理之类的东西?例如...如果我的应用程序在OnStartup
和之间崩溃会发生什么OnExit
?
采纳答案by Lorenzo Dematté
1) It looks like a standard Dispose implementation to me. It is not really necessary (see point 6) but it does not do any harm. (Cleanup on closing it's a bit like cleaning the house before burning it down, IMHO, but opinions on the matter differs..)
1)对我来说,它看起来像一个标准的 Dispose 实现。这并不是真正必要的(见第 6 点),但它不会造成任何伤害。(关闭时的清理有点像在烧毁之前清理房子,恕我直言,但对此事的看法不同..)
Anyway, why not using "Dispose" as the name of the cleanup method, even if it does not get called directly? You could have called it "Cleanup", but remember you also write code for humans, and Dispose looks familiar and anyone on .NET understands what is it for. So, go for "Dispose".
无论如何,为什么不使用“Dispose”作为清理方法的名称,即使它没有被直接调用?您可以将其称为“清理”,但请记住,您还为人类编写代码,而 Dispose 看起来很熟悉,任何使用 .NET 的人都了解它的用途。所以,去“处置”。
2) I have always seen m_Mutex = new Mutex(false, mutexName);
I think it's more a convention that a technical advantage, however.
2)我一直认为m_Mutex = new Mutex(false, mutexName);
我认为这更像是一种约定而不是技术优势。
3) From MSDN:
3)来自MSDN:
If the message is successfully registered, the return value is a message identifier in the range 0xC000 through 0xFFFF.
如果消息注册成功,则返回值为 0xC000 到 0xFFFF 范围内的消息标识符。
So I would not worry. Usually, for this class of functions, UInt is not used for "it does not fit in Int, let's use UInt so we have something more" but to clarify a contract "function never returns a negative value".
所以我不会担心。通常,对于此类函数,UInt 不是用于“它不适合 Int,让我们使用 UInt 以便我们有更多东西”,而是为了澄清约定“函数永远不会返回负值”。
4) I would avoid calling it if you will shutdown, same reason as #1
4)如果你要关机,我会避免调用它,与#1的原因相同
5) There are a couple of ways of doing it. The easiest way in Win32 is simply to have the second instance make the call to SetForegroundWindow (Look here: http://blogs.msdn.com/b/oldnewthing/archive/2009/02/20/9435239.aspx); however, I don't know if there is an equivalent WPF functionality or if you need to PInvoke it.
5)有几种方法可以做到。Win32 中最简单的方法就是让第二个实例调用 SetForegroundWindow(请看这里:http: //blogs.msdn.com/b/oldnewthing/archive/2009/02/20/9435239.aspx);但是,我不知道是否有等效的 WPF 功能,或者您是否需要 PInvoke 它。
6)
6)
For example... what happens if my application crashes between OnStartup and OnExit?
例如...如果我的应用程序在 OnStartup 和 OnExit 之间崩溃会发生什么?
It's OK: when a process terminates, all handles owned by the process are released; the mutex is released as well.
没关系:当一个进程终止时,该进程拥有的所有句柄都被释放;互斥量也被释放。
In short, my recommendations:
简而言之,我的建议是:
- I would used an approach based on named synchronization objects: it is the more established on the windows platform(s). (Be careful when considering a multi-user system, like terminal server! Name the synchronization object as a combination of, maybe, user name/SID and application name)
- Use the Windows API to raise the previous instance (see my link at point #5), or the WPF equivalent.
- You probably do not have to worry about crashes (kernel will decrease the ref counter for the kernel object for you; do a little test anyway), BUT If I may suggest an improvement: what if your first application instance does not crash but hangs? (Happens with Firefox.. I'm sure it happened to you too! No window, ff process, you cannot open a new one). In that case it may be good to combine another technique or two, to a) test if the application/window responds; b) find the hung instance and terminate it
- 我会使用一种基于命名同步对象的方法:它在 Windows 平台上更为成熟。(在考虑多用户系统时要小心,比如终端服务器!将同步对象命名为用户名/SID 和应用程序名称的组合)
- 使用 Windows API 引发前一个实例(请参阅我在第 5 点的链接)或 WPF 等效项。
- 您可能不必担心崩溃(内核会为您减少内核对象的引用计数器;无论如何做一点测试),但是如果我可以提出改进建议:如果您的第一个应用程序实例没有崩溃而是挂起怎么办?(发生在 Firefox 上。我敢肯定它也发生在你身上!没有窗口,ff 进程,你不能打开一个新的)。在这种情况下,结合另一种或两种技术可能会很好,以 a) 测试应用程序/窗口是否响应;b) 找到挂起的实例并终止它
For example, you can use your technique (trying to send/post a message to the window - if does not answer back it is stuck), plus MSK technique, to find and terminate the old process. Then start normally.
例如,您可以使用您的技术(尝试向窗口发送/发布消息 - 如果不回复则卡住),加上 MSK 技术,来查找并终止旧进程。然后正常启动。
回答by Luuk
I've used a simple TCP socket for this (in Java, 10 years ago).
我为此使用了一个简单的 TCP 套接字(在 Java 中,10 年前)。
- On startup connect to a predefined port, if the connection is accepted, another instance is running, if not, start a TCP Listener
- Once someone connects to you, popup the window and disconnect
- 启动时连接到一个预定义的端口,如果连接被接受,另一个实例正在运行,如果没有,启动一个 TCP 监听器
- 一旦有人连接到您,弹出窗口并断开连接
回答by blaise
The most straight forward way to handle that would be using a named semaphore. Try something like this...
最直接的处理方法是使用命名信号量。尝试这样的事情......
public partial class App : Application
{
Semaphore sema;
bool shouldRelease = false;
protected override void OnStartup(StartupEventArgs e)
{
bool result = Semaphore.TryOpenExisting("SingleInstanceWPFApp", out sema);
if (result) // we have another instance running
{
App.Current.Shutdown();
}
else
{
try
{
sema = new Semaphore(1, 1, "SingleInstanceWPFApp");
}
catch
{
App.Current.Shutdown(); //
}
}
if (!sema.WaitOne(0))
{
App.Current.Shutdown();
}
else
{
shouldRelease = true;
}
base.OnStartup(e);
}
protected override void OnExit(ExitEventArgs e)
{
if (sema != null && shouldRelease)
{
sema.Release();
}
}
}
回答by C-va
There are Several choices,
有几种选择,
- Mutex
- Process manager
- Named Semaphore
- Use a listener socket
- 互斥体
- 流程经理
- 命名信号量
- 使用侦听器套接字
Mutex
互斥体
Mutex myMutex ;
private void Application_Startup(object sender, StartupEventArgs e)
{
bool aIsNewInstance = false;
myMutex = new Mutex(true, "MyWPFApplication", out aIsNewInstance);
if (!aIsNewInstance)
{
MessageBox.Show("Already an instance is running...");
App.Current.Shutdown();
}
}
Process manager
流程经理
private void Application_Startup(object sender, StartupEventArgs e)
{
Process proc = Process.GetCurrentProcess();
int count = Process.GetProcesses().Where(p=>
p.ProcessName == proc.ProcessName).Count();
if (count > 1)
{
MessageBox.Show("Already an instance is running...");
App.Current.Shutdown();
}
}
Use a listener socket
使用侦听器套接字
One way to signal another application is to open a Tcp connection to it. Create a socket, bind to a port, and listen on a background thread for connections. If this succeeds, run normally. If not, make a connection to that port, which signals the other instance that a second application launch attempt has been made. The original instance can then bring its main window to the front, if appropriate.
向另一个应用程序发出信号的一种方法是打开一个到它的 Tcp 连接。创建一个套接字,绑定到一个端口,并在后台线程上监听连接。如果成功,则正常运行。如果没有,则连接到该端口,这会向另一个实例发出第二次应用程序启动尝试的信号。如果合适,原始实例然后可以将其主窗口置于最前面。
“Security” software / firewalls might be an issue.
“安全”软件/防火墙可能是一个问题。
回答by ZakiMa
I wanted to have a bit better user experience - if another instance is already running let's activate it rather than showing an error about the second instance. Here is my implementation.
我想要更好的用户体验 - 如果另一个实例已经在运行,让我们激活它而不是显示关于第二个实例的错误。这是我的实现。
I use named Mutex for making sure that only one instance is running and named EventWaitHandle to pass notification from one instance to another.
我使用命名互斥体来确保只有一个实例正在运行,并使用命名为 EventWaitHandle 将通知从一个实例传递到另一个实例。
App.xaml.cs:
应用程序.xaml.cs:
/// <summary>Interaction logic for App.xaml</summary>
public partial class App
{
#region Constants and Fields
/// <summary>The event mutex name.</summary>
private const string UniqueEventName = "{GUID}";
/// <summary>The unique mutex name.</summary>
private const string UniqueMutexName = "{GUID}";
/// <summary>The event wait handle.</summary>
private EventWaitHandle eventWaitHandle;
/// <summary>The mutex.</summary>
private Mutex mutex;
#endregion
#region Methods
/// <summary>The app on startup.</summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The e.</param>
private void AppOnStartup(object sender, StartupEventArgs e)
{
bool isOwned;
this.mutex = new Mutex(true, UniqueMutexName, out isOwned);
this.eventWaitHandle = new EventWaitHandle(false, EventResetMode.AutoReset, UniqueEventName);
// So, R# would not give a warning that this variable is not used.
GC.KeepAlive(this.mutex);
if (isOwned)
{
// Spawn a thread which will be waiting for our event
var thread = new Thread(
() =>
{
while (this.eventWaitHandle.WaitOne())
{
Current.Dispatcher.BeginInvoke(
(Action)(() => ((MainWindow)Current.MainWindow).BringToForeground()));
}
});
// It is important mark it as background otherwise it will prevent app from exiting.
thread.IsBackground = true;
thread.Start();
return;
}
// Notify other instance so it could bring itself to foreground.
this.eventWaitHandle.Set();
// Terminate this instance.
this.Shutdown();
}
#endregion
}
And BringToForeground in MainWindow.cs:
和 MainWindow.cs 中的BringToForeground:
/// <summary>Brings main window to foreground.</summary>
public void BringToForeground()
{
if (this.WindowState == WindowState.Minimized || this.Visibility == Visibility.Hidden)
{
this.Show();
this.WindowState = WindowState.Normal;
}
// According to some sources these steps gurantee that an app will be brought to foreground.
this.Activate();
this.Topmost = true;
this.Topmost = false;
this.Focus();
}
And add Startup="AppOnStartup" (thanks vhanla!):
并添加 Startup="AppOnStartup"(感谢 vhanla!):
<Application x:Class="MyClass.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Startup="AppOnStartup">
<Application.Resources>
</Application.Resources>
</Application>
Works for me :)
对我有用:)
回答by Hamed
This is a simple solution, Open your startup file (View from where your application starts) in this case its MainWindow.xaml. Open your MainWindow.xaml.cs file. Go to the constructor and after intializecomponent() add this code:
这是一个简单的解决方案,在本例中打开您的启动文件(从您的应用程序启动的位置查看)它的 MainWindow.xaml。打开 MainWindow.xaml.cs 文件。转到构造函数并在 intializecomponent() 之后添加以下代码:
Process Currentproc = Process.GetCurrentProcess();
Process[] procByName=Process.GetProcessesByName("notepad"); //Write the name of your exe file in inverted commas
if(procByName.Length>1)
{
MessageBox.Show("Application is already running");
App.Current.Shutdown();
}
Don't forget to add System.Diagnostics
不要忘记添加 System.Diagnostics
回答by smedasn
For WPF just use:
对于 WPF,只需使用:
public partial class App : Application
{
private static Mutex _mutex = null;
protected override void OnStartup(StartupEventArgs e)
{
const string appName = "MyAppName";
bool createdNew;
_mutex = new Mutex(true, appName, out createdNew);
if (!createdNew)
{
//app is already running! Exiting the application
Application.Current.Shutdown();
}
base.OnStartup(e);
}
}
回答by Dave_cz
Here is example that brings the old instance to foreground aswell:
这是将旧实例也带到前台的示例:
public partial class App : Application
{
[DllImport("user32", CharSet = CharSet.Unicode)]
static extern IntPtr FindWindow(string cls, string win);
[DllImport("user32")]
static extern IntPtr SetForegroundWindow(IntPtr hWnd);
[DllImport("user32")]
static extern bool IsIconic(IntPtr hWnd);
[DllImport("user32")]
static extern bool OpenIcon(IntPtr hWnd);
private static Mutex _mutex = null;
protected override void OnStartup(StartupEventArgs e)
{
const string appName = "LinkManager";
bool createdNew;
_mutex = new Mutex(true, appName, out createdNew);
if (!createdNew)
{
ActivateOtherWindow();
//app is already running! Exiting the application
Application.Current.Shutdown();
}
base.OnStartup(e);
}
private static void ActivateOtherWindow()
{
var other = FindWindow(null, "!YOUR MAIN WINDOW TITLE HERE!");
if (other != IntPtr.Zero)
{
SetForegroundWindow(other);
if (IsIconic(other))
OpenIcon(other);
}
}
}
But it will only work if your main window title do not change durig runtime.
但它只有在您的主窗口标题不改变 durig 运行时才有效。
Edit:
编辑:
You can also use Startup
event in App.xaml
instead of overriding OnStartup
.
您还可以使用Startup
event inApp.xaml
而不是覆盖OnStartup
.
// App.xaml.cs
private void Application_Startup(object sender, StartupEventArgs e)
{
const string appName = "LinkManager";
bool createdNew;
_mutex = new Mutex(true, appName, out createdNew);
if (!createdNew)
{
ActivateOtherWindow();
//app is already running! Exiting the application
Application.Current.Shutdown();
}
}
// App.xaml
<Application x:Class="MyApp.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MyApp"
StartupUri="MainWindow.xaml" Startup="Application_Startup"> //<- startup event
Remember to not call base.OnStartup(e)
in this case!
base.OnStartup(e)
在这种情况下记住不要打电话!
回答by BananaAcid
to prevent a second instance (and signal the existing),
防止第二个实例(并发出现有信号),
- using EventWaitHandle (since we are talking about an event),
- using Task,
- no Mutex code required,
- no TCP,
- no Pinvokes,
- no GarbageCollection stuff,
- thread save
- simple
- 使用 EventWaitHandle(因为我们在谈论一个事件),
- 使用任务,
- 不需要互斥代码,
- 没有TCP,
- 没有 Pinvokes,
- 没有垃圾收集的东西,
- 线程保存
- 简单的
it could be done like this (this for an WPF app (see ref to App()), but works on WinForms as well):
可以这样做(这适用于 WPF 应用程序(参见 App() 的参考),但也适用于 WinForms):
public partial class App : Application
{
public App()
{
// initiate it. Call it first.
preventSecond();
}
private const string UniqueEventName = "{GENERATE-YOUR-OWN-GUID}";
private void preventSecond()
{
try
{
EventWaitHandle.OpenExisting(UniqueEventName); // check if it exists
this.Shutdown();
}
catch (WaitHandleCannotBeOpenedException)
{
new EventWaitHandle(false, EventResetMode.AutoReset, UniqueEventName); // register
}
}
}
Second version: above plus signaling the other instance to show the window (change the MainWindow part for WinForms):
第二个版本:上面加上信号另一个实例来显示窗口(更改 WinForms 的 MainWindow 部分):
public partial class App : Application
{
public App()
{
// initiate it. Call it first.
//preventSecond();
SingleInstanceWatcher();
}
private const string UniqueEventName = "{GENERATE-YOUR-OWN-GUID}";
private EventWaitHandle eventWaitHandle;
/// <summary>prevent a second instance and signal it to bring its mainwindow to foreground</summary>
/// <seealso cref="https://stackoverflow.com/a/23730146/1644202"/>
private void SingleInstanceWatcher()
{
// check if it is already open.
try
{
// try to open it - if another instance is running, it will exist , if not it will throw
this.eventWaitHandle = EventWaitHandle.OpenExisting(UniqueEventName);
// Notify other instance so it could bring itself to foreground.
this.eventWaitHandle.Set();
// Terminate this instance.
this.Shutdown();
}
catch (WaitHandleCannotBeOpenedException)
{
// listen to a new event (this app instance will be the new "master")
this.eventWaitHandle = new EventWaitHandle(false, EventResetMode.AutoReset, UniqueEventName);
}
// if this instance gets the signal to show the main window
new Task(() =>
{
while (this.eventWaitHandle.WaitOne())
{
Current.Dispatcher.BeginInvoke((Action)(() =>
{
// could be set or removed anytime
if (!Current.MainWindow.Equals(null))
{
var mw = Current.MainWindow;
if (mw.WindowState == WindowState.Minimized || mw.Visibility != Visibility.Visible)
{
mw.Show();
mw.WindowState = WindowState.Normal;
}
// According to some sources these steps are required to be sure it went to foreground.
mw.Activate();
mw.Topmost = true;
mw.Topmost = false;
mw.Focus();
}
}));
}
})
.Start();
}
}
This code as a drop in class, will be @ Selfcontained-C-Sharp-WPF-compatible-utility-classes/Utils.SingleInstance.cs
此代码作为类中的下降,将是@ Selfcontained-C-Sharp-WPF-compatible-utility-classes / Utils.SingleInstance.cs
回答by ffetech
My Solution for a .Net Core 3 Wpf Single Instance Application:
我的 .Net Core 3 Wpf 单实例应用程序解决方案:
[STAThread]
public static void Main()
{
StartSingleInstanceApplication<CntApplication>();
}
public static void StartSingleInstanceApplication<T>()
where T : RichApplication
{
DebuggerOutput.GetInstance();
Assembly assembly = typeof(T).Assembly;
string mutexName = $"SingleInstanceApplication/{assembly.GetName().Name}/{assembly.GetType().GUID}";
Mutex mutex = new Mutex(true, mutexName, out bool mutexCreated);
if (!mutexCreated)
{
mutex = null;
var client = new NamedPipeClientStream(mutexName);
client.Connect();
using (StreamWriter writer = new StreamWriter(client))
writer.Write(string.Join("\t", Environment.GetCommandLineArgs()));
return;
}
else
{
T application = Activator.CreateInstance<T>();
application.Exit += (object sender, ExitEventArgs e) =>
{
mutex.ReleaseMutex();
mutex.Close();
mutex = null;
};
Task.Factory.StartNew(() =>
{
while (mutex != null)
{
using (var server = new NamedPipeServerStream(mutexName))
{
server.WaitForConnection();
using (StreamReader reader = new StreamReader(server))
{
string[] args = reader.ReadToEnd().Split("\t", StringSplitOptions.RemoveEmptyEntries).ToArray();
UIDispatcher.GetInstance().Invoke(() => application.ExecuteCommandLineArgs(args));
}
}
}
}, TaskCreationOptions.LongRunning);
typeof(T).GetMethod("InitializeComponent").Invoke(application, new object[] { });
application.Run();
}
}