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 8631631
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:14:42+00:00 2026-06-12T09:14:42+00:00

I have a .net Console application which runs infinitely. i have inserted Thread.Sleep() for

  • 0

I have a .net Console application which runs infinitely. i have inserted Thread.Sleep() for say 5 minutes to pause my application.
at this point of time i want to display user remaining time (in seconds) for thread to become active and start of application again.

class Program
{
    static void Main(string[] args)
    {
        Program program = new Program();
        program.startFeed();
    }
    public void startFeed()
    {
        while (true)
        {
            try
            {
                //My Application which i want to run continously 
                //when thread enters in run mode
            }
            catch (Exception xObj)
            {
                Console.WriteLine(DateTime.Now.ToString() 
                    + " >> Incoming Message Processing Error. >> " 
                    + xObj.Message);
            }
            Console.WriteLine("Waiting For Data......");
            Thread.Sleep(300000);
        }
    }
}
  • 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-12T09:14:42+00:00Added an answer on June 12, 2026 at 9:14 am

    The current structure of your program does not support what you are trying to achieve.

    When you call Thread.Sleep(int), it suspends the current thread for the specified time. By default, your program only runs as a single thread, so suspending that thread stops all your code from being run.

    You want to display updates to the user, but you also want your worker process to wait for 5 minutes between runs. This means that you will need to create a separate thread for the worker process, and manage that from the thread your program starts with.

    One of the simpler ways to do this is to use System.Threading.Timer, as others have suggested. In your case, you could change your code to something like this:

    class Program
    {
        static void Main(string[] args)
        {
            Program program = new Program();
            program.startFeed();
        }
    
        // This is run on the main thread
        public void startFeed()
        {
            // Start a Timer on a new thread to do work with the ProcessData method
            // Pass null to its 'state' argument, wait 0 milliseconds before
            // running it, and run it once every 300000 milliseconds
            using (new Timer(ProcessData, null, 0, 300000))
            {
                // The Timer will only exist while we are inside the 'using' block;
                // stay here with a loop
                while (true)
                {
                    // Write our status message
                    Console.WriteLine("Waiting for data at {0}...", DateTime.Now);
                    // We don't want this loop running ALL the time; add a small
                    // delay so it only updates once every second
                    Thread.Sleep(1000); 
                }
            }
        }
    
        // This is run on the background thread
        private void ProcessData(object state)
        {
            try
            {
                //My Application which i want to run continously 
                //when thread enters in run mode
            }
            catch (Exception xObj)
            {
                Console.WriteLine(DateTime.Now.ToString()
                    + " >> Incoming Message Processing Error. >> "
                    + xObj.Message);
            }
        }
    }
    

    Great, so now you have two threads running at the same time, and calling Thread.Sleep(int) on the one doesn’t influence the other. Note that you don’t need to call Thread.Sleep(int) in ProcessData, because the Timer takes care of that for you.

    Finally, you want to show the user exactly when ProcessData will run again. You could do this by adding a DateTime field to your Program class, say private DateTime _lastRun; and at the beginning of ProcessData, you could set it to DateTime.Now. Then, in the loop in startFeed, you could work out how many seconds are left until the next run using something like _lastRun.AddMinutes(5).Subtract(DateTime.Now).Seconds.


    There is a lot more that could be said here. As others have hinted, you are writing polling code instead of event-driven code. Polling is usually slower, less efficient and more complex than the event-driven equivalent. However, it depends on your data source being able to notify your code when it has new data to process; polling might be your only option.

    There is also a lot to be said about communication between threads. In and of itself, multi-threading is a very tricky subject and is the cause of many hard-to-find bugs. However, for this example, writing to the Console and setting a shared DateTime field should be safe across the two threads.

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

Sidebar

Related Questions

I have a simple .NET application which runs as Windows Service. Say it has
i have a rabbitmq consumer application in .net, which runs perfectly as a console
I have following requirement, I have C#/.Net console application, which refers to 'System.Data.Sqlite.dll' 'System.Data.Sqlite.dll'
I have a console application which has target .NET 2.0 It is very short
I have a .net application which runs in the region of 20 to 30
I have a C# .Net console application which calls a C++ .Net class library.
I have a console application which runs hundred times a day and reads same
I have a C++ .NET console application for which I changed the icon that
I have a .NET console application that needs to generate some HTML files. I
I have a .net Console Application called FooConsole. When I build and deploy it,

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.