Wrox Home  
Search P2P Archive for: Go

  Return to Index  

pro_vb thread: Process wait in VB


Message #1 by ramesh sridharan <mr_sriramesh@y...> on Tue, 19 Feb 2002 10:57:09 -0800 (PST)
If you are dealing with shelled commands such as copying files or running
other programs then you should follow this method.

Make a reference to: Windows Script Host Object Model

Then simply:

Dim ShellWait as New IwshShell_Class
Dim lngRetVal as Long
Dim strCommand as String

StrCommand = "c:\winnt\notepad.exe"
LngRetVal = ShellWait.Run(strCommand, vbNormalFocus, vbTrue)

Text1.text = Str(lngRetVal)

This will stop execution of the program until the shelled command completes
or is closed by the user. 

If you are dealing with copying memory or other tasks that do not require
shelling to another process, then it can get much harder.  Unfortunately I
can't think of any good examples of a process that would not be finished
before the next line of code executes, without using a shell command of some
sort.  We will just call it a long week.

Basically if this was the case then you might be stuck with using a sleep
command or trying to identify the handle of the process and then using the
WaitForSingleObject API call to wait for it to finish.  Honestly I have
never had to do this, I have used the above method for shelled commands on
numerous occasions.

I hope this helps, if not I'm sorry.  If you could send me the code that is
in question I could see if I could trap the process handle and test the
WaitForSingleObject method to see f I can get it to work.

Good luck.

Thanks

Crane Whitehead
Programmer / Analyst
cwhitehead@h...
xxx.xxx.xxxx
 
Healthaxis
Computing and Network services
http://www.healthaxisasg.com


 -----Original Message-----
From: 	Shannon Stewart [mailto:sstewart_59102@y...] 
Sent:	Tuesday, February 19, 2002 1:20 PM
Subject:	Re: Process wait in VB

Here's some code that I used in a project that I
developed.

Private Type STARTUPINFO
   cb As Long
   lpReserved As String
   lpDesktop As String
   lpTitle As String
   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

Private Type PROCESS_INFORMATION
   hProcess As Long
   hThread As Long
   dwProcessID As Long
   dwThreadID As Long
End Type

Private Declare Function WaitForSingleObject Lib
"kernel32" (ByVal _
   hHandle As Long, ByVal dwMilliseconds As Long) As
Long

Private Declare Function CreateProcessA Lib "kernel32"
(ByVal _
   lpApplicationName As Long, ByVal lpCommandLine As
String, ByVal _
   lpProcessAttributes As Long, ByVal
lpThreadAttributes As Long, _
   ByVal bInheritHandles As Long, ByVal
dwCreationFlags As Long, _
   ByVal lpEnvironment As Long, ByVal
lpCurrentDirectory As Long, _
   lpStartupInfo As STARTUPINFO, lpProcessInformation
As _
   PROCESS_INFORMATION) As Long

Private Declare Function CloseHandle Lib "kernel32"
(ByVal _
   hObject As Long) As Long

Private Const STARTF_USESHOWWINDOW& = &H1
Private Const NORMAL_PRIORITY_CLASS = &H20&
Private Const INFINITE = -1&

Public Sub ExecCmd(cmdline$)
   Dim proc As PROCESS_INFORMATION
   Dim start As STARTUPINFO
   Dim ReturnValue As Integer

   ' Initialize the STARTUPINFO structure:)
  With start
      .cb = Len(start)
      .dwflags = STARTF_USESHOWWINDOW
      .wShowWindow = 0
  End With

   ' Start the shelled application:
   ReturnValue = CreateProcessA(0&, cmdline$, 0&, 0&,
1&, _
      NORMAL_PRIORITY_CLASS, 0&, 0&, start, proc)
  
  
   ' Wait for the shelled application to finish:
   Do
      ReturnValue = WaitForSingleObject(proc.hProcess,
0)
      DoEvents
   Loop Until ReturnValue <> 258

   ReturnValue = CloseHandle(proc.hProcess)
End Sub

Sub Testing()
   ExecCmd (Enter the command you want to run here)
End Sub

It will start a process and continue when this process
is finished.

Hope this helps,

sstewart

--- ramesh sridharan <mr_sriramesh@y...> wrote:
> Hi,
>   Im just working on an application which copies the
> contents in screen and pastes. But since the
> application runs very fast the subsequent commands
> are
> getting executed before this copying process
> completes
> successfully. But i'm getting correct results if i
> keep a message box in the middle. Can you give a
> command or so which waits till the current process
> ends. Please help me in this regard.
> 
> =====
> Sincerely,
> Ramesh S
> 
>
************************************************************
> *   Intellectuals solve problems; geniuses prevent
> them.   *
>
************************************************************
> 
> __________________________________________________
> Do You Yahoo!?
> Yahoo! Sports - Coverage of the 2002 Olympic Games
> http://sports.yahoo.com
> 

  Return to Index