[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


How to refresh main form when batch is running

Posted on 2004-11-10
Medium Priority
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
  • 2
LVL 86

Accepted Solution

Mike Tomlinson earned 2000 total points
ID: 12549397
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


Author Comment

ID: 12554961
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 86

Expert Comment

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


Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month20 days, 8 hours left to enroll

868 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