How to refresh main form when batch is running

I have a program that launches a batch file that does some processing. If I open another application and or my application loses the focus and the new window opens on top of my application the main form does not refresh or redraw itself. I also get the message to "switch to another program..".

My batch uses the "WaitForSingleObject" API to wait for the batch to terminate.

How can I fix this problem?

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mike TomlinsonMiddle School Assistant TeacherCommented:
Something like this:

Option Explicit

Private Const SYNCHRONIZE = &H100000
Private Const WAIT_TIMEOUT = &H102

Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, _
            ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, _
            ByVal dwMilliseconds As Long) As Long
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long

Private Sub Command1_Click()
    Dim lPid As Long
    Dim lHnd As Long
    Dim lRet As Long
    Dim exitCode As Long

    Command1.Enabled = False

    lPid = Shell("notepad.exe", vbNormalFocus)
    If lPid <> 0 Then
        lHnd = OpenProcess(SYNCHRONIZE, 0, lPid)
        If lHnd <> 0 Then
                DoEvents ' keep application responsive
                Sleep 50 ' keep CPU usage from ramping to 100%
            Loop Until WaitForSingleObject(lHnd, 0) <> WAIT_TIMEOUT
            CloseHandle (lHnd)
        End If
    End If
    Command1.Enabled = True
    MsgBox "Done"
End Sub


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ampapaAuthor Commented:
When the batch is running I set the mouse pointer to vbhourglass but yet I'm able to select items on my form is there a way to stop this?
Mike TomlinsonMiddle School Assistant TeacherCommented:
Right, changing the pointer won't prevent clicks on the form.  You can disable the whole thing with Me.Enabled = False if you like, or manually disable/enabled invdividual controls as necessary.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.