检查进程是否正在运行,然后切换到它? [英] Check process is running, then switch to it?

查看:75
本文介绍了检查进程是否正在运行,然后切换到它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我单击Button1时,我具有以下代码来检查"chrome"是否正在运行.如果没有,它将启动chrome.这可以工作,但是我不知道If语句中已经运行的chrome所需的代码.希望这是我很想念的简单事情.

I have the below code to check if 'chrome' is running when I click Button1. If not it launches chrome. This works but I dont know the code needed in the If statement to switch to the chrome if its already running. Hopefully this is something very simple i am missing.

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    If Process.GetProcessesByName("chrome").Count > 0 Then

       ??**SHOW RUNNING APPLICATION**??

    Else
        Process.Start("C:\Program Files\Google\Chrome\Application\chrome.exe")
    End If


End Sub

推荐答案

正如我在上面的评论Chrome starts many instances of itself. Each tab has its own process, so how are you going to tell it which one to switch to?中所提到的.这取决于您最小化窗口或将其自身最小化到任务栏时选择的选项卡.下面应该可以帮助您,并且已经过尝试和测试.唯一的问题是,如果您打开Chrome并有多个选项卡就可以了,但是如果您创建Chrome的另一个实例,它将不会显示第二个实例,而只会显示第一个实例...如果您关闭第一个例子,第二个例子当然会出现.

As I mentioned in my above comment, Chrome starts many instances of itself. Each tab has its own process, so how are you going to tell it which one to switch to?. This come's down to what tab was selected when you minimize the window or it minimizes itself to the task bar. Below should help you out and it's tried and tested. The only issue is, if you open Chrome and have multiple tabs it's fine, but if you create another instance of Chrome it will not show the second instance, it will only bring forward the first instance... If you close the first instance, the second instance of course will come forward.

Public Class Form1

#Region "DLL Imports"
    <System.Runtime.InteropServices.DllImport("User32.dll")> _
    Private Shared Function SetForegroundWindow(handle As IntPtr) As Boolean
    End Function

    <System.Runtime.InteropServices.DllImport("User32.dll")> _
    Private Shared Function ShowWindow(handle As IntPtr, nCmdShow As Integer) As Boolean
    End Function

    <System.Runtime.InteropServices.DllImport("User32.dll")> _
    Private Shared Function IsIconic(handle As IntPtr) As Boolean
    End Function
#End Region

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
       StartOrShowProcess("chrome")
    End Sub


    Private Sub StartOrShowProcess(ByVal strProcessName As String)
        Try
            Dim handle As IntPtr
            Dim proc As Process() = Process.GetProcessesByName(strProcessName)
            If proc.Count > 0 Then
                For Each procP As Process In proc
                    handle = procP.MainWindowHandle
                    If handle <> 0 AndAlso IsIconic(handle) Then 'Do we have a handle and is it minimized?
                        ShowWindow(handle, 9)
                        SetForegroundWindow(handle)
                    End If
                Next
            Else 'Not running or started...
                Process.Start(strProcessName)
            End If

        Catch ex As Exception
            'Handle your error...
        End Try
    End Sub


End Class

这篇关于检查进程是否正在运行,然后切换到它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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