如何通过按键中断程序

问题描述 投票:0回答:1

我想通过按T键来中断我的生产者-消费者程序。我搜索了很多答案,但是我不知道为什么]它不起作用。

static void Main(string[] args)
    {
    Buffer buffer = new Buffer();
    Produtor prod = new Produtor(buffer);
    Thread threadProdutor = prod.CriarThreadProdutor();
    Consumidor cons = new Consumidor(buffer, 100000);
    Thread threadConsumidor = cons.CriarThreadConsumidor();

    threadProdutor.Start();
    threadConsumidor.Start();

    threadProdutor.Join();
    threadConsumidor.Join();
    while (Console.KeyAvailable && Console.ReadKey(true).Key == ConsoleKey.T)
    {
        Environment.Exit(0);
    }
}

我在一段时间内添加了一个断点,但是该程序甚至无法到达那里。

我想通过按T键来中断我的生产者-消费者程序。我搜索了很多答案,但我不知道为什么它不起作用。静态void Main(string [] args){缓冲区...

c# multithreading monitor
1个回答
0
投票

通过在您的Join()调用之后放入循环,线程将在您检查控制台进行输入时已经完成,因此您需要颠倒顺序。

© www.soinside.com 2019 - 2024. All rights reserved.