1

I have to automate repetitive tasks of a windows application (no API), using VB.Net.

What I did is simulate key strokes usually done by user. I'm using System.Threading, and initiating the application using a new process.

My PROBLEM is, that this app pops a message box when a task is done, and this message box sure disables the main app window, so I need to know when this message box is displayed to simulate the ENTER key stroke and proceed to next task.

Here is a sample of my code:

Imports System.Threading

Public Class Form1
    Dim a As New Process

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        a.StartInfo.FileName = "C:\Program Files (x86)\CE\CE.exe"
        a.Start()
        AppActivate(a.Id)

        For i = 1 To CInt(txt.Text)
            DoTask()
        Next
    End Sub

    Private Sub DoTask()
        Thread.Sleep(1000)

        SendKeys.SendWait("^(N)")
        Thread.Sleep(500)
        SendKeys.SendWait("{ENTER}")
        Thread.Sleep(500)
        SendKeys.SendWait("new check")
        Thread.Sleep(500)
        SendKeys.SendWait("{ENTER}")

        Do While True
            'Here I want to wait until a message box pops up by the application
            'If message box displayed then exit do
        Loop

        SendKeys.SendWait("{ENTER}")
    End Sub
End Class

Please help me with this. Thank you

Prisoner
  • 1,839
  • 2
  • 22
  • 38
Saleem
  • 709
  • 2
  • 13
  • 34

1 Answers1

2

I personally use AutoIt for this kind of tasks - there is a function WinWaitActive which waits until a window with specified title or class name is open and active and then you can send simply keystrokes to it.

See https://www.autoitscript.com/autoit3/docs/functions/WinWaitActive.htm

In VB I think you would have to do something like this: wait for a window with specified title or class name to become visible and activated.

Determine Whether Program is the Active Window in .NET

If you need to stick with VB you can import and use AutoIt DLL:

Running AutoIt3 Scripts in VB 2010

There is a nice tool within AutoIt editor that lets you examine windows and their child controls and use class names within your script. For example I have written code that waits until ALV grid with resultset is displayed within SAP window:

Func _WaitForALVGrid()
    Sleep(5000)
    _WinWaitActivate($windowName,"")
    Local $count = 0
    Do
        Sleep(5000) 
        $count = $count + 1
        $Text=ControlGetText("[CLASS:SAP_FRONTEND_SESSION]","","[CLASS:Afx:63DE0000:8:00010003:00000010:00000000]")
        if StringLeft($Text, 6) = "Nebyla" Then Return False 
        if $count>360 Then Return False
    Until StringInStr(WinGetClassList($windowName), "SapALVGrid")>0
    _WinWaitActivate($windowName,"")
    Sleep(5000)
    Return True
EndFunc

EDIT:

There is also a method in VB.NET that does not use AutoIT - similar to WinWaitActivate:

http://forums.codeguru.com/showthread.php?460402-C-General-How-do-I-activate-an-external-Window

Class Program
    <DllImportAttribute("User32.dll")> _
    Private Shared Function FindWindow(ClassName As [String], WindowName As [String]) As Integer
    End Function
    <DllImportAttribute("User32.dll")> _
    Private Shared Function SetForegroundWindow(hWnd As Integer) As IntPtr
    End Function
    Private Shared Sub Main(args As String())
        '**************************** CODE YOU SEEK?
        'Find the window, using the CORRECT Window Title, for example, Notepad            
        Dim hWnd As Integer = 0
        While hWnd = 0
            Thread.Sleep(1000)
            hWnd = FindWindow(Nothing, "Untitled - Notepad") 
        End While
        'If found activate it
        SetForegroundWindow(hWnd)
        '**************************** CODE YOU SEEK?
    End Sub
End Class

Another good example is here:

close a message box from another program using c#

Community
  • 1
  • 1
Vojtěch Dohnal
  • 7,867
  • 3
  • 43
  • 105
  • You are welcome. This type of programming is something that every reasonable coder tries to avoid, but sometimes there is no other way - company policies, closed source code etc. – Vojtěch Dohnal Jul 12 '14 at 14:25
  • You're right. And from my testing, I still have some worries about the performance, it seems impossible to make my program work without worrying about an error, unless no user do any action on pc while it's being executed! – Saleem Jul 12 '14 at 23:05