-1

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
Randomizers
  • 71
  • 1
  • 1
  • 7
  • Okay. Write some code to do that. If you have a problem with it, post the code and explain the problem. – Blackwood Nov 11 '16 at 18:47
  • Using a Timer would be one way to do that. Go for it. – topshot Nov 11 '16 at 18:48
  • @Randomizers : It is better if you edit your question and include your new code. It is almost impossible to read in the comments. Also please explain what doesn't work. – Visual Vincent Nov 11 '16 at 19:07
  • @VisualVincent I ended up fixing it, however I don't think this is the best way of doing this, can you guys suggest me a better way of doing this? Check OP – Randomizers Nov 11 '16 at 19:17
  • 1
    What if you change your mind and don't want to play the game anymore but you already opened your app? How would you exit? – djv Nov 11 '16 at 19:39
  • _I tried doing like this, and it works lol, is there a better way of doing this?_ - than this question is for [http://codereview.stackexchange.com/](http://codereview.stackexchange.com/) – Fabio Nov 11 '16 at 22:49
  • @Verdolino I don't know that's why I'm asking you guys. Thanks – Randomizers Nov 11 '16 at 23:33
  • 1
    @Fabio : His initial question was _how_ to do it, so it was more fit for Stack Overflow at first. – Visual Vincent Nov 11 '16 at 23:45
  • @Randomizers : This is more or less the closest to the "best" way you'll get using a .NET Framework language. Alternatively you can use [**WMI**](http://stackoverflow.com/a/857946/3740093). – Visual Vincent Nov 11 '16 at 23:50

1 Answers1

2

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
djv
  • 15,168
  • 7
  • 48
  • 72
  • Thanks a lot, it's a winform application. However the part where the user has to press retry isn't what I need. CSGO launches in fullscreen so the user would have to tab down and press retry in this case. I just want the tool to loop til it finds csgo and then continue automatically. An options to close the exit instead of retry would be nice. – Randomizers Nov 12 '16 at 22:31