Mutex类被误解了,而Global mutex则更是如此.
在创建全局互斥锁时使用什么是好的,安全的模式?
一个会起作用的
在编程时我是初学者,但我确信其中一个通用规则是程序以Main()开头.我在创建WPF项目时没有看到一个.Main()只是在WPF中以不同的方式命名?
这是我到目前为止实现的代码,用于创建单个实例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;
} …Run Code Online (Sandbox Code Playgroud) 我已经在公共MainWindow(){}中放了一些代码,但是一旦我这样做,我就不断得到一些模糊的XAML解析错误(不是在我的计算机上,而是在其他3个我尝试过的 - 是的!)
是否有应用程序启动时运行代码AS ASON的首选方法?
理论是我希望它打电话回家并且问它可以开始.如果不是,我希望该应用程序关闭.称之为临时复制保护:)