wpf的单进程
来源:互联网 发布:淘宝交易截图生成器 编辑:程序博客网 时间:2024/05/22 02:02
转自:http://blogs.microsoft.co.il/blogs/arik/archive/2010/05/28/wpf-single-instance-application.aspx
The Problem
The question this post solves is how to enforce that your WPF application has only one instance?
Solution Source
The solution is based on code found in some WPF reference applications which Microsoft will soon (?) release. I didn’t wrote it, but I used it several times and it’s the best solution I’ve found to date, so I'd hate to see it unpublished.
Solution Advantages
So, what are the advantages of this solution? after all, it’s not the first time someone posts a solution for this problem.
Well, the most important advantage is that it works.
No glitches. No special cases. No inherent race conditions.
It simply works.
Second, it’s easily used. On the next section I’ll show you exactly how to use it.
Third, there’s no constraints on your WPF application. Specifically, your main application / window class doesn’t have to inherit some base class for this to work.
And at last, you don’t need to be dependent on any VB DLL.
I say this because one of the popular solutions for this problem requires your to add a reference to a (WinForms related!) Visual Basic DLL, which may feel strange for some people.
As a bonus, the solution provides to the first instance the parameters of the second instance when run. This is very useful if you want to integrate you application with the Windows 7 taskbar.
Solution Details
So, let’s see how to make your WPF application having just one instance.
Step 1: Add the file SingleInstance.cs to your project.
Step 2: Add a reference to your project: System.Runtime.Remoting
Step 3: Have your application class implement ISingleInstanceApp (defined in SingleInstance.cs).
The only method in this interface is:
bool SignalExternalCommandLineArgs(IList<string> args)
This method is called when a second instance of your application tries to run. It has an args parameter which is the same as the command line arguments passed to the second instance.
Step 4: Define your own Main function that uses the single instance class.
Your App class should now be similar to this:
/// Interaction logic for App.xaml
/// </summary>
publicpartialclassApp
:
Application,
ISingleInstanceApp{
privateconststring
Unique =
"My_Unique_Application_String";
[STAThread]
public static void Main()
{
if (SingleInstance<App>.InitializeAsFirstInstance(Unique))
{
var application = new App();
application.InitializeComponent();
application.Run();
// Allow single instance code to perform cleanup operations
SingleInstance<App>.Cleanup();
}
}
#region ISingleInstanceApp Members
public bool SignalExternalCommandLineArgs(IList<string> args)
{
// handle command line arguments of second instance
// ...
return true;
}
#endregion
}
Step 5: Set new main entry point
Select Project Properties –> Application and set “Startup object” to your App class name instead of “(Not Set)”.
Step 6: Cancel the default WPF main function
Right-click on App.xaml, Properties, set Build Action to "Page" instead of "Application Definition".
Solution Inner Works
I can summarize it as: Mutex and Remoting, done right.
If you want to know more details, just have a look at the code.
You can find a WPF sample application here.
That’s it for now,
Arik Poznanski.
- wpf的单进程
- WPF单步调试程序的设置
- C# 禁用进程内的按键 wpf
- 单节点kubernetes的进程
- WPF实现单进程实例应用程序(像Word一样可以打开多个文件)
- WPF单实例
- WPF 单实例方法
- WPF单实例限制
- WPF:如何实现单实例的应用程序(Single Instance)
- 测试浏览器是否单进程和单线程的代码
- 单进程单线程的Redis如何能够高并发
- 单处理器系统的进程调度
- 守护进程的单实例实现
- 守护进程的单实例实现
- 单进程socket的服务器/客户端模型
- C#中进程单例的实现
- 守护进程的单实例实现
- shell单进程执行cmd的函数
- ManualResetEvent
- (转)Chrome源码剖析 【序】 && 【一】
- TCP网络编程
- .NET 线程池编程技术
- AES算法源码
- wpf的单进程
- (转)Chrome源码剖析【二】
- js正则;js表单验证
- 找不到**.dll模块
- 安装包制作
- 利用Poderosa建立SSH远程操作
- Linux 下安装wireshark
- 用“魔法师”调整分区
- 开发琐事11.24