从SysTray还原应用程序而不是启动NEW实例?

时间:2014-04-29 05:00:20

标签: vb.net

有没有人知道如何获取已经运行的应用程序实例(最小化到系统托盘)来恢复而不是运行新实例?

我已将项目属性调整为"制作单实例应用程序"。这非常适合阻止新应用程序启动,但不能恢复已经运行的实例。

我在Windows 7 Pro SP1上的Visual Studio Express 2012中使用VB.NET。

我还没有任何代码,但是这里是我用来最小化到托盘的代码(如在这个伟大的论坛上找到的),以防相关:

    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As FormClosingEventArgs) Handles Me.FormClosing
    Me.WindowState = FormWindowState.Minimized
    Me.Visible = False
    e.Cancel = True
    nfi.Visible = True
End Sub

Private Sub ShowWindowToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ShowWindowToolStripMenuItem.Click
    nfi.Visible = False
    Me.WindowState = FormWindowState.Maximized
    Me.Visible = True
End Sub

2 个答案:

答案 0 :(得分:2)

使用StartupNextInstance event,它会在您的程序再次启动时触发。

Project + Properties,Application选项卡,单击View Application Events按钮。在编辑器窗口顶部的左上角组合框中选择(MyApplication Events)。在右侧组合框中选择“StartupNextInstance”。使它看起来类似于:

Partial Friend Class MyApplication
    Private Sub MyApplication_StartupNextInstance(sender As Object, e As ApplicationServices.StartupNextInstanceEventArgs) Handles Me.StartupNextInstance
        Form1.Show()
    End Sub
End Class

答案 1 :(得分:1)

我能想到的最好的方法是确保如果启动任何其他实例,新实例可以提醒用户它。

您应该能够使用此代码:

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim ProcessName As String = Process.GetCurrentProcess.ProcessName.ToString()
    Dim AllProcesses As List(Of System.Diagnostics.Process) = Process.GetProcessesByName(ProcessName).ToList()
    Dim ToKillNow As List(Of System.Diagnostics.Process) = OtherProcesses(AllProcesses)
    If IsAlreadyRunning() Then
        If vbOK = MsgBox("There are already one or more windows of this program running, you can terminate the window(s) now and continue running this one by pressing 'Ok'. Otherwise, click 'Cancel' and you can use the other one(s).", MsgBoxStyle.OkCancel, "Another Instance of This Program is Running") Then
            'Updates the list, more instances may have been started since we last checked
            AllProcesses = Process.GetProcessesByName(ProcessName).ToList()
            ToKillNow = OtherProcesses(AllProcesses)
            'Do the actual killing
            For Each Item In ToKillNow
                If Not Item.HasExited = True Then
                    Item.Kill()
                End If
            Next
        Else
            End
        End If
    End If
End Sub

Public Function IsAlreadyRunning()
    Dim ProcessName As String = Process.GetCurrentProcess.ProcessName.ToString()
    Dim AllProcesses As List(Of System.Diagnostics.Process) = Process.GetProcessesByName(ProcessName).ToList()
    Dim CurrentProcessId As Integer = Process.GetCurrentProcess.Id
    For Each p As Process In AllProcesses
        If Not p.Id = CurrentProcessId Then
            Return True
        End If
    Next
    Return False
End Function

Public Function OtherProcesses(ByVal ListOfProcesses As List(Of System.Diagnostics.Process))
    Dim ToKillNow As New List(Of System.Diagnostics.Process)
    Dim CurrentProcessId As Integer = Process.GetCurrentProcess.Id
    For Each Item In ListOfProcesses
        If Not Item.Id = CurrentProcessId Then
            ToKillNow.Add(Item)
        End If
    Next
    Return ToKillNow
End Function

希望这会有所帮助:)

相关问题