我在VB里调用osql初始化sqlserver1数据库,然后需要在程序里连接上新初始化的数据库添加数据。现在用shell或者ShellExecute后都有同样的问题就是调用osql初始化数据库需要花费不少时间,而新开的进程又不能设置成模态的,所以Vb里的后继程序无法同步。我希望能用某种方式打开一个osql,等到它完全运行完毕后继续执行后面的程序。
如果有大侠能知道还望赐教,尽量说的详细一些。
另外就是我暂不考虑加timer等待这样的方法,实在没有前途了再说。
如果有大侠能知道还望赐教,尽量说的详细一些。
另外就是我暂不考虑加timer等待这样的方法,实在没有前途了再说。
Public Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As LongFunction StillRun(ByVal ProgramID) As Boolean
Dim lHProgram As Long
Dim lReturn As Long
Dim hProgram As Long
hProgram = 0
hProgram = OpenProcess(0, False, ProgramID)
If Not hProgram = 0 Then
StillRun = True
Else
StillRun = False
End If
CloseHandle hProgram
End Function
--------------------------------------------------------------------
使用:
pID = Shell(调用的程序)
While StillRun(pID)
' DoEvents
Wend
Const INFINITE = &HFFFFFFFF
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long' 程序范例:(以执行 Notepad 程序为例)Private Sub Command1_Click()
Dim pId As Long, pHnd As Long ' 分别声明 Process Id 及 Process Handle
pId = Shell("Notepad", vbNormalFocus) ' Shell 传回 Process Id
pHnd = OpenProcess(SYNCHRONIZE, 0, pId) ' 取得 Process Handle
If pHnd <> 0 Then
Call WaitForSingleObject(pHnd, INFINITE) ' 无限等待,直到程序结束
Call CloseHandle(pHnd)
End IfEnd Sub