?
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
?
307 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 58

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 58

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Suggested Courses

764 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