如何在C#中使用布尔值暂时暂停程序执行,并在布尔值为true时恢复它?

huangapple go评论72阅读模式
英文:

How can I temporarily halt program execution using a bool in C# and resume it when the bool is true?

问题

如何在布尔值(codepause)为false时“暂停”程序(就像在VS中调试时设置断点一样),然后当codepause为true时,程序“恢复”(就像在调试中按继续一样)?

我还没有尝试过任何操作,因为我不知道该怎么做。

英文:

How do you "pause" a program (like a breakpoint) while a bool (codepause) is false, then when codepause is true the program "resumes" (as if you pressed continue while debugging in VS)?

I haven't tried anything yet as I don't know what to do.

答案1

得分: -2

你可以使用一个布尔变量与循环结合,暂时停止程序执行,直到布尔条件为真。这里有一个示例:

using System;
class Program
{
    static bool isPaused = true;

    static void Main(string[] args)
    {
        Console.WriteLine("Press 'p' to pause and 'r' to resume.");
        Console.WriteLine("Enter 'q' to quit the program.");

        // 启动一个单独的线程来监视暂停/恢复输入,
        // 同时主线程继续执行。
        var inputThread = new System.Threading.Thread(ReadInput);
        inputThread.Start();

        // 主程序循环
        while (true)
        {
            // 检查程序是否被暂停
            while (isPaused)
            {
                // 等待一小段时间后再次检查
                System.Threading.Thread.Sleep(100);
            }

            // 恢复程序执行
            Console.WriteLine("Program resumed");
            
            // 在这里执行你的程序逻辑

            // 如果输入 'q',则退出程序
            if (Console.ReadKey().KeyChar == 'q')
                break;
        }

        // 在退出前停止输入线程
        inputThread.Join();
    }

    static void ReadInput()
    {
        while (true)
        {
            // 读取用户输入
            var key = Console.ReadKey().KeyChar;

            // 如果输入 'p',则暂停程序
            if (key == 'p')
            {
                isPaused = true;
                Console.WriteLine("Program paused");
            }

            // 如果输入 'r',则恢复程序
            if (key == 'r')
            {
                isPaused = false;
                Console.WriteLine("Program resumed");
            }

            // 如果输入 'q',则退出程序
            if (key == 'q')
                break;
        }
    }
}
英文:

you can use a boolean variable in combination with a loop to temporarily halt program execution until the boolean condition is true.

Here's an example:

 using System;
 class Program
{
static bool isPaused = true;

static void Main(string[] args)
{
    Console.WriteLine("Press 'p' to pause and 'r' to resume.");
    Console.WriteLine("Enter 'q' to quit the program.");

    // Start a separate thread to monitor the pause/resume input
    // while the main thread continues executing.
    var inputThread = new System.Threading.Thread(ReadInput);
    inputThread.Start();

    // Main program loop
    while (true)
    {
        // Check if the program is paused
        while (isPaused)
        {
            // Wait for a short duration before checking again
            System.Threading.Thread.Sleep(100);
        }

        // Resume program execution
        Console.WriteLine("Program resumed");
        
        // Perform your program logic here

        // Exit the program if 'q' is entered
        if (Console.ReadKey().KeyChar == 'q')
            break;
    }

    // Stop the input thread before exiting
    inputThread.Join();
}

static void ReadInput()
{
    while (true)
    {
        // Read user input
        var key = Console.ReadKey().KeyChar;

        // Pause the program if 'p' is entered
        if (key == 'p')
        {
            isPaused = true;
            Console.WriteLine("Program paused");
        }

        // Resume the program if 'r' is entered
        if (key == 'r')
        {
            isPaused = false;
            Console.WriteLine("Program resumed");
        }

        // Quit the program if 'q' is entered
        if (key == 'q')
            break;
    }
}
 }

huangapple
  • 本文由 发表于 2023年5月26日 12:54:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76337760.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定