英文:
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;
    }
}
 }
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论