Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...