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

The Archive Base Latest Questions

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

This is the solution I came up with to pause/resume an active thread inside

  • 0

This is the solution I came up with to pause/resume an active thread inside a console…

It requires a second thread for input, which the main loop reacts too. But my ignorance of C# makes me wonder if there is a simpler solution? Perhaps one that can be done with the main thread alone?

Essentially, I am testing several things that will iterate at the defined FPS, and I want to be able to pause and resume the iteration via keyboard input. Any feedback is appreciated.

class TestLoops
{
    int targetMainFPS = 5;
    int targetInputFPS = 3;
    bool CONTINUE = true;

    Thread testLoop, testLoop2;
    ConsoleKeyInfo cki;

    ManualResetEvent resetThread = new ManualResetEvent(true); //How to correctly pause threads in C#.
        public void Resume() { resetThread.Set(); }
        public void Pause() { resetThread.Reset(); }

    public TestLoops()
    {
        //_start = DateTime.Now.Ticks;
        Console.Write("CreatingLoop...");

        this.testLoop = new Thread(MainLoop);
        this.testLoop.Start();

        this.testLoop2 = new Thread(InputLoop);
        this.testLoop2.Start();
    }

    void MainLoop()
    {
        long _current = 0;
        long _last = 0;

        Console.Write("MainLoopStarted ");
        while(CONTINUE)
        {
            resetThread.WaitOne();

            _current = DateTime.Now.Ticks / 1000;
            if(_current > _last + (1000 / targetMainFPS) )
            {
                _last = _current;

                //Do something...
                Console.Write(".");

            }
            else
            {
                System.Threading.Thread.Sleep(10);
            }
        }
    }

    void InputLoop()
    {
        long _current = 0;
        long _last = 0;

        Console.Write("InputLoopStarted ");
        while(CONTINUE)
        {
            _current = DateTime.Now.Ticks / 1000;
            if(_current > _last + (1000 / targetInputFPS))
            {
                _last = _current;

                //Manage keyboard Input
                this.cki = Console.ReadKey(true);
                //Console.Write(":");
                if(this.cki.Key == ConsoleKey.Q)
                {
                    //MessageBox.Show("'Q' was pressed.");
                    CONTINUE = false;
                }

                if(this.cki.Key == ConsoleKey.P)
                {
                    this.Pause();
                }
                if(this.cki.Key == ConsoleKey.R)
                {
                    this.Resume();
                }
            }
            else
            {
                System.Threading.Thread.Sleep(10);
            }
        }
    }

    public static void Main(string[] args)
    {
        TestLoops test = new TestLoops();
    }

}
  • 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-28T07:24:16+00:00Added an answer on May 28, 2026 at 7:24 am

    You can simplify it drastically by using the non-blocking Console.KeyAvailable check. That way, you can execute all your code from the main thread:

    using System;
    using System.Threading;
    
    class TestLoops
    {
        const int targetFPS = 5;
    
        public static void Main(string[] args)
        {
            bool _continue = true;
            DateTime _last = DateTime.MinValue;
    
            while (_continue)
            {
                if (Console.KeyAvailable)
                {
                    ConsoleKeyInfo cki = Console.ReadKey(true);
    
                    if (cki.Key == ConsoleKey.Q)
                    {
                        Console.WriteLine("\n'Q' was pressed.");
                        _continue = false;
                    }
    
                    if (cki.Key == ConsoleKey.P)
                    {
                        Console.WriteLine("\n'P' was pressed; press 'R' to resume.");
    
                        // Block until 'R' is pressed.
                        while (Console.ReadKey(true).Key != ConsoleKey.R)
                            ; // Do nothing.
    
                        Console.WriteLine("'R' was pressed.");
                    }
                }
    
                DateTime _current = DateTime.Now;
                if (_current - _last > TimeSpan.FromMilliseconds(1000F / targetFPS))
                {
                    _last = _current;
    
                    // Do something...
                    Console.Write(".");
                }
                else
                {
                    System.Threading.Thread.Sleep(10);
                }
            }        
        }
    }
    

    Edit: Replying to comment.

    Be careful. The code below blocks due to the Console.ReadKey call, not due to the while loop. The while loop is there only so that, if the user enters a character other than R, the program would discard it and wait for another character to be entered.

    // Block until 'R' is pressed.
    while (Console.ReadKey(true).Key != ConsoleKey.R)
        ; // Do nothing.
    

    If you wanted to block until any character is pressed, you would simply use:

    Console.ReadKey(true);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I Google'd not long ago and came across this solution to loop out information
is this solution possible in asp.net dragging picturebox inside winform on runtime i just
I have this solution for a single button: myButton.Attributes.Add(onclick, this.disabled=true; + GetPostBackEventReference(myButton).ToString()); Which works
I found this solution which works, but I find it hard to believe there
Question below. This is the solution I came up with based on Pixeler's answer.
I came up with this solution. But this looks too complicated. There must be
This was just what I was thinking for this solution and I would like
if your app use this solution , do do you plan port the app
I am currently stuck in the design of this solution. The data layer design
Sorry for the Windows developers out there, this solution is for Macs only. This

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.