好的,正如你在下面的代码中看到的,我有一个简单的try语句。 每当我打开工具时,它将寻找一个名为csgo的进程,如果有一个进程,它将继续使用该工具,否则它将只发送一个MsgBox并退出该工具。
但是,我希望它能够检查是否有csgo进程正在运行,如果它正在运行它应该像现在一样继续运行,但是如果没有运行名为csgo的进程运行我&#39 ; d想要使工具Sleep和loop直到它找到一个名为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
我尝试这样做,并且它有效,大声笑,有没有更好的方法呢?
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
答案 0 :(得分:2)
假设这是一个控制台应用程序,您可以对所拥有的内容进行一些改进。
' 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