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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

790 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