How to refresh main form when batch is running

Posted on 2004-11-10
Last Modified: 2010-05-02
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?

Question by:ampapa
    LVL 85

    Accepted Solution

    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

    LVL 8

    Author Comment

    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?
    LVL 85

    Expert Comment

    by:Mike Tomlinson
    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.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
    Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
    As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
    Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now