?
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
Medium Priority
?
310 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 61

Assisted Solution

by:HainKurt
HainKurt earned 2000 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 61

Accepted Solution

by:
HainKurt earned 2000 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 86

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

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

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

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses
Course of the Month15 days, 6 hours left to enroll

839 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