我正在尋找一種方法來執行Steam(Steam.exe)並等待它被加載(不退出)。執行一個應用程序,並等待它加載
我認爲一個好主意是列出所有子句柄並在標題句柄中搜索一個字符串,因爲當蒸汽加載存在名爲「朋友」的窗口句柄時,但這很難做到這一點(API ),所以也許存在一個簡單的方法來等待程序加載......什麼,我會做
例子:
' First process
Process.start("Process 1.exe")
' Here will goes an efficient method to wait for application is loaded,
' not sleeping X seconds measuring the loading time or any of that.
' Second process
' Depends on first process fully loaded.
' If first process is not fully loaded (libraries and that) then this process can't run.
Process.start("Processs 2.exe")
UPDATE:
主要的問題是如何等待X程序被加載,但對Steam.exe當蒸汽加載嘗試讀取/打開一些REGKEYS我注意到太:
http://img267.imageshack.us/img267/6599/prtscrcapture4u.jpg
也許我可以做用代碼來監視某個RegKeys是否被認可?
像這樣的事情?:
Dim RegKey = "HKLM\...blablabla"
Process.start("steam.exe")
While not Regkey.IsAccessed
' Do nothing and wait
Loop
Process.start("Second process.exe")
更新2:
我試圖做一個函數以供參考Amegon的解決方案,我沒有完成的代碼因爲我得到一行錯誤:
Memory = hprocess.PrivateMemorySize64
但是,當我嘗試推出一個「大」的應用程序(應用程序需要大量的時間來加載,如Photoshop),與「小」的應用程序工作好,我只能得到錯誤。
如果有人可以幫助我簡化/改進Amegon的解決方案,使通用功能和糾正內存溢出錯誤...謝謝!
這是我的代碼:
' This is the sub that launchs the unfinished function:
Private Sub Launch_Game()
'Wait_For_Application_To_Load("C:\Games\Steam.exe", "-applaunch 0")
Wait_For_Application_To_Load("C:\Program Files\Adobe Photoshop CS6 (64 Bit)\Photoshop.exe")
End Sub
Private Function Wait_For_Application_To_Load(ByVal APP_Path As String, Optional ByVal APP_Arguments As String = Nothing)
Dim File = My.Computer.FileSystem.GetFileInfo(APP_Path)
Process.Start(APP_Path, APP_Arguments)
Timer_CheckCPU.Tag = File.Name.Substring(0, File.Name.Length - 4) ' Photoshop
Timer_CheckCPU.Enabled = True
End Function
Private Declare Function ReadProcessMemory Lib "kernel32" (ByVal hProcess As Integer, ByVal lpBaseAddress As Integer, ByVal lpBuffer As Integer, ByVal nSize As Integer, ByRef lpNumberOfBytesWritten As Integer) As Integer
Private WithEvents Timer_CheckCPU As New Timer
Dim Memory_Value_Changed As Boolean
Dim CPU_Changed As Boolean
Dim CPU_Time As Boolean
Dim Running_Time As Boolean
Private _desiredTime_ms As Integer = 1500
Private Sub Timer_CheckCPU_Tick(sender As Object, ev As EventArgs) Handles Timer_CheckCPU.Tick
Timer_CheckCPU.Enabled = False
Dim pProcess() As Process = System.Diagnostics.Process.GetProcessesByName(Timer_CheckCPU.Tag)
Dim hprocess As Process = pProcess(0)
If hprocess Is Nothing Then
Running = False
Timer_CheckCPU.Enabled = True
Return
End If
Running = True
' Here is the error:
Memory = hprocess.PrivateMemorySize64
' MsgBox(hprocess.PrivateMemorySize64.ToString)
CPUTotal = hprocess.TotalProcessorTime.TotalMilliseconds
If AllConditionsGood() Then
If Not (_countdown.IsRunning) Then
_countdown.Reset()
_countdown.Start()
End If
Dim _elapsed As Integer = _countdown.ElapsedMilliseconds
If _elapsed >= _desiredTime_ms Then
Me.TopMost = True
MsgBox("process loaded")
Return
End If
Else
_countdown.Reset()
End If
Timer_CheckCPU.Enabled = True
End Sub
Private Function AllConditionsGood() As Boolean
If CPU_Time Then Return False
If Memory_Value_Changed Then Return False
If Running_Time Then Return False
Return True
End Function
Private _countdown As New Stopwatch
Private _Running As Boolean = False
Public WriteOnly Property Running() As Boolean
Set(ByVal value As Boolean)
_Running = value
If value Then
Running_Time = False
Else
Running_Time = True
End If
End Set
End Property
Private _CPUTotal As Integer
Public WriteOnly Property CPUTotal() As Integer
Set(ByVal value As Integer)
CPU = value - _CPUTotal 'used cputime since last check
_CPUTotal = value
End Set
End Property
Private _CPU As Integer
Public WriteOnly Property CPU() As Integer
Set(ByVal value As Integer)
If value = 0 Then
CPU_Time = False
Else
CPU_Time = True
End If
_CPU = value
End Set
End Property
Private _Memory As Integer
Public WriteOnly Property Memory() As Integer
Set(ByVal value As Integer)
MemoryDiff = Math.Abs(value - _Memory)
_Memory = value
End Set
End Property
Private _MemoryDiff As Integer
Public WriteOnly Property MemoryDiff() As Integer
Set(ByVal value As Integer)
If value = _MemoryDiff Then
Memory_Value_Changed = False
Else
Memory_Value_Changed = True
End If
_MemoryDiff = value
End Set
End Property
你檢查了Process類的[GetProcessesByName](http://msdn.microsoft.com/en-us/library/z3w4xdc9.aspx)方法嗎?只需嘗試使用像ProcessExplorer這樣的工具來識別進程名稱..... – Steve 2013-04-09 16:15:29
如果點擊進程按鈕,您會看到什麼?你正在搜索的過程不是窗口 – Steve 2013-04-09 16:26:08
對不起,這是我第一次使用spy ++,我編輯了我的問題! – ElektroStudios 2013-04-09 16:30:33