VB.NET:WPF 单实例 [英] VB.NET: WPF Single Instance

查看:30
本文介绍了VB.NET:WPF 单实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有人使用 VB.NET 2010 能够创建单实例应用程序?我遵循了 MSDN 示例,但它没有 Application.xaml 文件.

Has anyone using VB.NET 2010 been able to create a single instance application? I've followed the MSDN sample but it does not have an Application.xaml file.

将任何 C# 示例转换为 VB 都不起作用,因为我无法覆盖 Application.xaml 中的 Main sub(C# 将其称为 App.xaml).

Converting any C# samples to VB doesn't work as I cannot override the Main sub in Application.xaml (C# calls it App.xaml).

推荐答案

您可以尝试使用互斥锁.在项目属性中,禁用应用程序框架并将 Sub Main 设置为启动对象.然后在你的项目中添加一个模块:

You can try using a Mutex. In the projects properties, disable the application framework and set Sub Main as the startup object. Then add a Module to your project:

Imports System.Threading

Module EntryPoint
    Sub Main()
        Dim noPreviousInstance As Boolean

        Using m As New Mutex(True, "Some Unique Identifier String", noPreviousInstance)
            If Not noPreviousInstance Then
                MessageBox.Show("Application is already started!")
            Else
                Dim mainWindow As New MainWindow()
                Dim app As New Application()
                app.Run(mainWindow)
            End If
        End Using
    End Sub
End Module

使用此方法,您必须通过调用应用程序的 Shutdown 方法来处理应用程序的关闭.

With this method, you will have to take care of your app's shutdown by calling the Shutdown method of the application.

这篇关于VB.NET:WPF 单实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆