Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9115561
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T04:27:44+00:00 2026-06-17T04:27:44+00:00

I have a single Timer control on my form with the following Tick event

  • 0

I have a single Timer control on my form with the following Tick event handler:

private void timer1_Tick(object sender, EventArgs e) {
    foreach (char c in "Andrew") {
        SendKeys.SendWait(c.ToString());
        System.Threading.Thread.Sleep(1000);
    }
}

Since System.Windows.Forms.Timer runs on the UI thread, I would expect the event handler to block further Tick events occuring while it’s running which would give AndrewAndrewAndrew... as output. Instead, I get AnAnAnAn....

Why are subsequent Tick events raised and handled before the first has finished?

How can I make sure the Timer raises one event at a time, and gets fully blocked until the handler has run to completion?

I realise Thread.Sleep is a horrible way of timing code. I just want to know what is going on.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T04:27:45+00:00Added an answer on June 17, 2026 at 4:27 am

    You are victim of re-entry via the message loop. You are recursing into your timer1_Tick function indirectly via the message loop. What is happening is that inside SendKeys.SendWait another message loop is being spun up (not on a different thread) to monitor the whether the messages have been processed. Then on another thread, while messages are being processed in this inner loop, the timer is firing and posting a message to call to your tick function again. Hilarity ensues.

    Perhaps a simplified example will help. Run this and observe the output.

    public class Program
    {
        private static Queue<Action> queue = new Queue<Action>();
    
        public static void Main(string[] args)
        {
            // put three things in the queue. 
            // In a simple world, they would finish in order.
            queue.Enqueue(() => DoWork(1));
            queue.Enqueue(() => DoComplicatedWork(2));
            queue.Enqueue(() => DoWork(3));
    
            PumpMessages();            
        }
    
        private static void PumpMessages()
        {
            while (queue.Count > 0)
            {
                Action action = queue.Dequeue();
                action();
            }
        }
    
        private static void DoWork(int i)
        {
            Console.WriteLine("Doing work: {0}", i);
        }
    
        private static void DoComplicatedWork(int i)
        {
            Console.WriteLine("Before doing complicated work: {0}", i);
            PumpMessages();
            Console.WriteLine("After doing complicated work: {0}", i);
        }
    
    }`
    

    You are sort of assuming because there is only one thread pumping messages in the UI that each item that is queued is processed in order. However, this is not true when methods put into the queue can themselves pump messages. In the example, the 3rd operation actually completes before the 2nd one. The DoComplicatedWork method is analogous to what is happening in SendWait.

    I should answer your second question on how to prevent this. A lock won’t help because they are re-entrant (i.e. the same thread can acquire a lock more than once). The best way is to disable the timer or detach the tick handler while inside the method and re-enable/attach the handler again before returning. You could also just try a simple boolean flag to indicate whether you are in the method already and return if so.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have A single page that has the following structure <form runat=server><placeholder></placeholder></form> I have
I have a windows app which is just a form with a timer control
I have a Calendar control on a VB6 form which on one machine is
I have a DetailsView control that is bound to a single table in my
I currently have an application that spawns multiple instances of a single win form.
I have a single form window application now I want to change the form
I have a form built in ASP.NET. The first control allows the user to
I have a repeater that displays a custom user control on a form multiple
I have a form which has a search feature - a single text field
I have some custom logic that needs to be executed every single time a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.