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
301 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

910 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

25 Experts available now in Live!

Get 1:1 Help Now