vb.net 如果已经运行,则阻止应用程序启动

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18477502/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 14:49:39  来源:igfitidea点击:

Prevent application launch if already running

vb.netwinforms

提问by Jeremy

My app needs to check and see if it is already running when launching so it doesn't open a second time. I have a system tray icon that can make the application visible = False. Works great. However, I need to make sure the user looks at the system tray for the notifyicon if the app is already running.

我的应用程序需要在启动时检查它是否已经在运行,以便它不会再次打开。我有一个系统托盘图标,可以使应用程序可见 = False。效果很好。但是,如果应用程序已经在运行,我需要确保用户查看系统托盘中的通知图标。

Private Sub mainWindowSmall_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim p() As Process
        p = Process.GetProcessesByName("TSC Tool Box")
        If p.Count > 0 Then
            MessageBox.Show("The TSC Tool Box is already running. Check System tray!", _
            "Warning !!!", MessageBoxButtons.OK, _
            MessageBoxIcon.Error, MessageBoxDefaultButton.Button2)
            Me.Close()
        Else
            totalOnLoad()
        End If
    End Sub

VB.NET | winforms

VB.NET | winforms

回答by Joel Coehoorn

This feature is already built in to Windows Forms. Just go to the project properties and click the "Single Instance Application" checkbox. There's also a StartupNextInstanceevent that you can handle.

此功能已内置于 Windows 窗体中。只需转到项目属性并单击“单实例应用程序”复选框。还有一个您可以处理的StartupNextInstance事件。