Intervals to execute System.Threading.Thread.Sleep

1

Visual basic is driving me crazy. I'm trying to control an application already open by sending keystrokes. The code is as follows:

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
    Dim seqc As Process
    seqc = New Process()
    seqc = Process.Start("C:\seqc\seqc.exe")


    System.Threading.Thread.Sleep(1000) ' Wait 1 sec and hit enter on main screen
    AppActivate(seqc.Id)
    SendKeys.Send("{ENTER}")

    System.Threading.Thread.Sleep(1000) ' wait 1 sec, hit tab and hit enter in second screen
    SendKeys.Send("{TAB}")
    SendKeys.Send("{ENTER}")
End Sub

The problem is that most times it does not work. Increasing the waiting time, does not help either.

It seems that System.Threading.Thread.Sleep is not working properly.

Any ideas?

Thank you very much

    
asked by Pepo 05.09.2016 в 20:35
source

1 answer

1

I will give a direct answer to your question, although I share the idea that you should look for a more efficient way to do it, and without making technical clarifications, why it is not a good idea to use Syste.Threading.Threar.Sleep, I'll give you the code that I tried with notepad.exe

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load


    Dim seqc As Process
    seqc = New Process()
    seqc = Process.Start("C:\Windows\notepad.exe")


    espera(1000) ' Wait 1 sec and hit enter on main screen
    AppActivate(seqc.Id)
    SendKeys.Send("{ENTER}")

    espera(1000) ' wait 1 sec, hit tab and hit enter in second screen
    SendKeys.Send("{TAB}")
    SendKeys.Send("{ENTER}")
    SendKeys.Send(Keys.A)
End Sub

Function espera(ms As Long) As Boolean

    Dim cx As Long = 0
    Do While cx < ms

        System.Threading.Thread.Sleep(1)
        Application.DoEvents()
        cx = cx + 1
    Loop
    Return True
End Function
    
answered by 09.09.2016 в 21:38