Solved

Why would having a delay in the Timer1 code block greater then the Timer1 Interval case the Sendkeys to type slowly?

Posted on 2010-08-12
4
300 Views
Last Modified: 2012-05-10
I added a Timer from Toolbox with an Interval property of 1000.  

The code sends keystrokes to notepad.  If i place a sleep of less then 1000 it instantly prints the whole word out.

If increase the delay past 1000 it slowly types out one later at a time.

Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
        System.Threading.Thread.Sleep(1250)
        Dim ps() As Process = Process.GetProcessesByName("notepad")
        If ps.Length > 0 Then

            Dim p As Process = ps(0)
            Dim hWnd As IntPtr = p.MainWindowHandle

            SetForegroundWindow(hWnd)
            System.Windows.Forms.SendKeys.SendWait("Experts Exchange")
            System.Windows.Forms.SendKeys.SendWait("{Enter}")
        End If
    End Sub

Open in new window


I guess I expected with the delay being greater then the timer interval it would print Experts Exchange once every 1250 milliseconds.

Not the output of the keys being printed in slow motion.
0
Comment
Question by:vaultworld
  • 2
4 Comments
 
LVL 51

Assisted Solution

by:HainKurt
HainKurt earned 500 total points
ID: 33422835
on event, disable the timer
and enable at the end
Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick

  Timer2.enabled=false

  ...

  Timer2.enabled=true

End Sub

Open in new window

0
 
LVL 51

Accepted Solution

by:
HainKurt earned 500 total points
ID: 33422867
you are doing something wrong ;)
that timer triggers that code every second
and in the code you have some sleeps and some codes
after a while there will be multiple codes running at the same time making you confuse ;)
so, disable the timer, do your job, enable again

is this a one time job? if yes, then just disable at the beginning, so it will not run again...
0
 
LVL 1

Author Comment

by:vaultworld
ID: 33422935
I agree that is a viable solution.  But why the behavior?
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 33423199
It's SendWait() vs. Send() that causes some of the funny behavior.

Try this:

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Debug.Print("Entered Tick()")
        System.Threading.Thread.Sleep(5000)
        Dim ps() As Process = Process.GetProcessesByName("notepad")
        If ps.Length > 0 Then

            Dim p As Process = ps(0)
            AppActivate(p.Id)

            Debug.Print("Sending EE")
            System.Windows.Forms.SendKeys.SendWait("Experts Exchange")
            Debug.Print("Sending Enter")
            System.Windows.Forms.SendKeys.SendWait("{Enter}")
        End If
    End Sub

Note that it NEVER gets to "Sending Enter".

Now change the two SendWait() calls to Send() and see what happens....
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
This video discusses moving either the default database or any database to a new volume.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

747 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

11 Experts available now in Live!

Get 1:1 Help Now