Answer the question
In order to leave comments, you need to log in
How to implement a function until a key is pressed?
How to implement until the key is pressed to execute the function. When pressed, finish execution, and after processing the keypress event, perform the same function.
Here's something like this.
do
{
do
{
//функция
} while (!Console.KeyAvailable);
//обработка события нажатия на клавишу
} while (Console.ReadKey().Key != ConsoleKey.Escape);
Answer the question
In order to leave comments, you need to log in
Create a CancellationTokenSource and take its Token :
var source = new CancellationTokenSource();
var token = source.Token;
Task.Run(() =>
{
do
{
}
while (!token.IsCancellationRequested);
});
source.Cancel();
It's not easy to do this on a form. Read about threads and events. At a minimum, it will be necessary to make a background thread with the ability to interrupt, which will do everything that is needed, otherwise the UI will freeze and it will not lead to anything good.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question