【问题标题】:Restore Application from SysTray rather than launch NEW instance?从 SysTray 恢复应用程序而不是启动新实例?
【发布时间】:2014-04-29 05:00:19
【问题描述】:

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

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

我在 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

【问题讨论】:

  • 如果单击 ShowWindow 菜单将恢复表单,但不会通过从开始菜单重新启动。我的困惑是当用户可能有效地尝试从菜单。据我了解,它们不会在当前应用程序的上下文中,或者如果启动另一个实例,原始应用程序是否可以捕获某些事件?
  • 使用 StartupNextInstance 事件。项目 + 属性,应用程序选项卡,单击查看应用程序事件按钮并添加事件处理程序。

标签: vb.net


【解决方案1】:

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

项目+属性,应用程序选项卡,单击查看应用程序事件按钮。在编辑器窗口顶部的左上角组合框中选择(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

【讨论】:

  • 感谢您的回答,听起来就像我要找的一样。我会试试看。附:我不是一个全职程序员,所以不经常写代码。
【解决方案2】:

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

您应该可以使用此代码:

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

希望这会有所帮助:)

【讨论】:

  • 感谢您的回复。我将首先查看 Hans 的答案(因为它更短),但如果它不完全符合我的要求,可能不得不回到这个答案。
猜你喜欢
  • 2011-12-08
  • 2013-11-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-08
  • 2020-10-28
  • 1970-01-01
  • 2021-01-04
  • 2020-04-26
相关资源
最近更新 更多