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

  • Home
  • SEARCH
  • 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 1003475
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T07:59:10+00:00 2026-05-16T07:59:10+00:00

I want a timer tick/elapsed event inside a new thread. It seems like I

  • 0

I want a timer tick/elapsed event inside a new thread. It seems like I cannot use the Windows timer. But if I use the Timers.Timer, it creates worked thread from the threadpool for each elapsed event. Is there a way to make these events to occur in the same thread?

Update:

Thank you everybody for answering this question.

My intention behind the whole thing might sound a little crazy though. I want you to correct me. This is my thinking(as a novice) when I am trying to acieve this. I am trying perform a task every 2 seconds. When I use Timers.Timer, it creates a thread every 2 seconds which I am thinking it as an overhead.

My main thread and my other threads need a lot of processor’s time to perform their tasks. So, if I can avoid creating these Threads, I am saving whatever microseconds of the processor to create a thread each time timer elapsed, for my main and other threads.

I made a quick tests and compared few solutions. 1000 msec interval in each case. 100 ticks.

Solution1: infinite loop with waits/sleep {00:01:42.0068344}

solution2: Using Brian’s Synchronizer {00:01:42.4068573}

solution3: Timers.Timer as it is {00:01:42.4018571}

This should tell me that 2.0068344, 2.4068573, 2.4018571 are the times wasted for other things in the background other than the time interval of 1000 msec for 100 ticks. This should mean when solution1 meet your needs, it is the best solution performance-wise??

This should also mean that though Brian’s solution is synchronized to one thread, it is in-fact creating threads in the background.

Please confirm it or correct me.

  • 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-05-16T07:59:11+00:00Added an answer on May 16, 2026 at 7:59 am

    Yes. You can make the System.Timers.Timer execute the Elapsed event handler in the same thread. To do this you need to set the SynchronizingObject property to an object that will marshal the execution onto a thread of your choosing. However, it is important to realize that you cannot just inject execution onto a random thread. The receiving thread has to be specifically designed to allow marshaling the execution of method calls.

    public static void Main(string[] args)
    {
        var timer = new System.Timers.Timer();
        timer.Interval = 1000;
        timer.Elapsed += 
          (s, a) => 
          { 
            Console.WriteLine("{1} {0}", 
              DateTime.Now, Thread.CurrentThread.ManagedThreadId); 
          };
        timer.SynchronizingObject = new Synchronizer();
        timer.Start();
        Console.ReadLine();
    }
    

    And here is the code for the Synchronizer class. This code uses the BlockingCollection class from 4.0 of the .NET BCL. If you are not using 4.0 then you can replace it with Stephen Toub’s BlockingQueue.

    public class Synchronizer : ISynchronizeInvoke
    {
        private Thread m_Thread;
        private BlockingCollection<Message> m_Queue = new BlockingCollection<Message>();
    
        public Synchronizer()
        {
            m_Thread = new Thread(Run);
            m_Thread.IsBackground = true;
            m_Thread.Start();
        }
    
        private void Run()
        {
            while (true)
            {
                Message message = m_Queue.Take();
                message.Return = message.Method.DynamicInvoke(message.Args);
                message.Finished.Set();
            }
        }
    
        public IAsyncResult BeginInvoke(Delegate method, object[] args)
        {
            Message message = new Message();
            message.Method = method;
            message.Args = args;
            m_Queue.Add(message);
            return message;
        }
    
        public object EndInvoke(IAsyncResult result)
        {
            Message message = result as Message;
            if (message != null)
            {
                message.Finished.WaitOne();
                return message.Return;
            }
            throw new ArgumentException("result");
        }
    
        public object Invoke(Delegate method, object[] args)
        {
            Message message = new Message();
            message.Method = method;
            message.Args = args;
            m_Queue.Add(message);
            message.Finished.WaitOne();
            return message.Return;
        }
    
        public bool InvokeRequired
        {
            get { return Thread.CurrentThread != m_Thread; }
        }
    
        private class Message : IAsyncResult
        {
            public Delegate Method = null;
            public object[] Args = null;
            public object Return = null;
            public object State = null;
            public ManualResetEvent Finished = new ManualResetEvent(false);
    
            public object AsyncState
            {
                get { return State; }
            }
    
            public WaitHandle AsyncWaitHandle
            {
                get { return Finished; }
            }
    
            public bool CompletedSynchronously
            {
                get { return false; }
            }
    
            public bool IsCompleted
            {
                get { return Finished.WaitOne(0); }
            }
        }
    }
    

    Update:

    You need to understand how System.Timers.Timer works. It actually uses System.Threading.Timer behind the scenes and executes the event handler on a ThreadPool thread. So it is not really creating any threads at all. The same ones from the thread pool are getting recycled over and over again. There is no thread creation overhead. That is the whole point of a thread pool.

    Now, the solution I provided here still uses the ThreadPool behind the scenes, but instead of having the work item execute the Elapsed event handler directly it is executing the Synchronizer.BeginInvoke method which marshals the event handler onto the thread that the Synchronizer created.

    So it should be no surpise that the solution I provided here would be slower. Personally, I would stick to solution #1 as it is easier. I would not worry a whole lot about performance at this point.

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

Sidebar

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.