Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
261 views
in Technique[技术] by (71.8m points)

process - Wait for a specefic program to open before continue VB.NET

Alright, as you see in the code down below I've got a simple try statement. Whenever I open the tool it will look for a process named csgo, if there is a process it will continue with the tool else it will just send a MsgBox and exit the tool.

However, I want it for it to check if there is a csgo process running, if it's running it should continue like it is now, but if there isn't a process named csgo running I'd like to make the tool Sleep and loop til it finds a process named csgo.

 Try
        gameProcess = Process.GetProcessesByName("csgo")(0)
        gameHandle = OpenProcess(PROCESS_ALL_ACCESS, False, csgoProcess.Id)
    Catch ex As Exception
        MsgBox("Please Start CS:GO before opening this tool")
        Environment.Exit(0)
    End Try

I tried doing like this, and it works lol, is there a better way of doing this?

  Dim Found = 0
    While Found = 0
        Try
            gameProcess = Process.GetProcessesByName("csgo")(0)
            cgameHandle = OpenProcess(PROCESS_ALL_ACCESS, False, csgoProcess.Id)
            Found = 1
        Catch ex As Exception
            MsgBox("Waiting for csgo to launch.")
            Threading.Thread.Sleep(1000)
        End Try
    End While
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Assuming this is a console application, you can make some improvements over what you have.

' Use a boolean for binary logic.
' This takes up 1 bit whereas your Int32 took up 32 bits
Dim allDone = False
While Not allDone
    Try
        Dim processes = Process.GetProcessesByName("csgo")
        If processes.Count > 0 Then
            csgoProcess = processes(0)
            Dim handle = OpenProcess(PROCESS_ALL_ACCESS, False, csgoProcess.Id)
            If handle IsNot Nothing Then
                allDone = True
            End If
        Else
            ' Use a Retry / Cancel MsgBox to allow the user to retry or cancel
            ' Clicking Cancel will now exit the application
            Select Case MsgBox("Waiting for csgo to launch.",
                               MsgBoxStyle.RetryCancel, "Confirm retry")
                Case MsgBoxResult.Retry
                    Threading.Thread.Sleep(1000)
                Case MsgBoxResult.Cancel
                    Environment.Exit(0)
            End Select
        End If
    Catch ex As Exception
        ' Something bad happened, but you don't know what exactly.
        ' You should exit, else it might keep happening
        MsgBox("Your application encountered the following error and will now close: " _
           & ex.Message)
        Environment.Exit(0)
    End Try
End While
' continue application

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...