起動したアプリがアイドル状態になるまで待機          <TOP>


CreateProcess プロセスの起動
WaitForInputIdle タイムアウト時間が経過するまで待機
 

例では、Exeに「Notepad.exe」を、Paramに意識して重いtextを読ませています。
 
'================================================================
'= 起動したアプリがアイドル状態になるまで待機
'=    (WaitForInputIdle.bas)
'================================================================
#include "Windows.bi"

Type PROCESS_INFORMATION
    hProcess    As Long
    hThread     As Long
    dwProcessId As Long
    dwThreadId  As Long
End Type

Type STARTUPINFO
    cb As Long
    lpReserved      As Long
    lpDesktop       As Long
    lpTitle         As Long
    dwX             As Long
    dwY             As Long
    dwXSize         As Long
    dwYSize         As Long
    dwXCountChars   As Long
    dwYCountChars   As Long
    dwFillAttribute As Long
    dwFlags         As Long
    wShowWindow     As Integer
    cbReserved2     As Integer
    lpReserved2     As Long
    hStdInput       As Long
    hStdOutput      As Long
    hStdError       As Long
End Type

' プロセスの起動
Declare Function Api_CreateProcess& Lib "kernel32" Alias "CreateProcessA" (ByVal Name$, ByVal Cmd$, pAtt As Any, tAtt As Any, ByVal Hand&, ByVal Flg&, Env As Any, ByVal Dir$, sInfo As Any, pInfo As PROCESS_INFORMATION)

' タイムアウト時間が経過するまで待機
Declare Function Api_WaitForInputIdle& Lib "user32" Alias "WaitForInputIdle" (ByVal hProcess&, ByVal dwMilliseconds&)

#define NORMAL_PRIORITY_CLASS &H20      '通常クラス(一般的なプロセス)
#define STARTF_USESHOWWINDOW &H1
#define SW_SHOW 5                       'ウィンドウをアクティブ化し現在の位置とサイズで表示

Var Shared Edit(1) As Object
Var Shared Text(1) As Object
Var Shared Button1 As Object

For i = 0 To 1
    Edit(i).Attach GetDlgItem("Edit" & Trim$(Str$(i + 1))) : Edit(i).SetFontSize 14
    Text(i).Attach GetDlgItem("Text" & Trim$(Str$(i + 1))) : Text(i).SetFontSize 14
Next
Button1.Attach GetDlgItem("Button1") : Button1.SetFontSize 14

Var Shared sPath As String
Var Shared sParam As String

'================================================================
'=
'================================================================
Declare Sub Button1_on edecl ()
Sub Button1_on()
    Var si As STARTUPINFO
    Var pi As PROCESS_INFORMATION
    Var Ret As Integer

    sPath = Edit(0).GetWindowText
    sParam = Edit(1).GetWindowText

    On Error GoTo *Err_Trap
    
    ShellEx = False
    
    si.cb = Len(si)
    si.dwFlags = STARTF_USESHOWWINDOW
    si.wShowWindow = SW_SHOW
    
    'アプリケーションを起動しプロセス作成する
    If Api_CreateProcess(ByVal 0, sPath & " " & sParam, ByVal 0, ByVal 0, 1, NORMAL_PRIORITY_CLASS, ByVal 0, ByVal 0, si, pi) = 0 Then GoTo *Err_Trap

    Do
        If Api_WaitForInputIdle(pi.hProcess, 100) = 0 Then Exit Do
    Loop
    
    ShellEx = True

    A% = MessageBox("", "アイドル状態になりました!", 0, 2)
    Exit Sub

    *Err_Trap
        A% = MessageBox("", "エラー", 0, 2)
End sub

'================================================================
'= シェルドロップされたファイル名を取得
'================================================================
Declare Sub Edit1_DropFiles edecl (ByVal DF As Long)
Sub Edit1_DropFiles(ByVal DF As Long)
    Var Ret As Long

    Ret = GetDropFileCount(DF)
    sPath = GetDropFileName(DF, 0)
    Edit(0).SetWindowText sPath
End Sub

'================================================================
'= シェルドロップされたパラメータを取得
'================================================================
Declare Sub Edit2_DropFiles edecl (ByVal DF As Long)
Sub Edit2_DropFiles(ByVal DF As Long)
    Var Ret As Long

    Ret = GetDropFileCount(DF)
    sParam = GetDropFileName(DF, 0)
    Edit(1).SetWindowText sParam
End Sub

'================================================================
'=
'================================================================
While 1
    WaitEvent
Wend
Stop
End