C # .NET: how to pause and resume a program when a key is pressed?

This is a .NET non GUI application. It is listed as a console application, but the console is not actually used. what the application does is test the graphical interfaces of other applications.

+3
source share
3 answers

Others described how you could start / stop the execution of the problem ... for the actual key capture, I would suggest registering a global hotkey - but you need to have a Windows form handle available to you, so I also suggest either running your command line utility from GUI applications, or simply incorporate functionality into a graphical application.

Is such a change possible?

EDIT: Christian Lienberger made great packaging for his blog .

+2
source

Console.Read() ( Console.ReadKey()) . , - (, WaitHandle).

- , / , - .

+1

. .

//Assuming the thread name given is 'newthread'   
//First Start a thread     

newthread.Start();   

//use Console Key Class to read a key from keyboard    
ConsoleKeyInfo cki = new ConsoleKeyInfo();  
cki = Console.ReadKey(true);

//I am using letter 'p' to pause    
if (cki.Key == ConsoleKey.P)   
{  
  newthread.Suspend();  
  Console.WriteLine("Process Is Paused,Press 'Enter' to Continue...");  
}  

//Thread Resumption can only occur through another thread   
//If the thread control has passed on to the main function    
//resume it from there    


// Resuming Thread if paused   
if (newthread.ThreadState>0)    
{    
  ConsoleKeyInfo ckm = new ConsoleKeyInfo();   
  ckm = Console.ReadKey(true);    
  if (ckm.Key == ConsoleKey.Enter)    
   {    
     Console.WriteLine("Resuming Process...");    
     newthread.Resume();    
   }       
}  

. , . , , . .

+1

Source: https://habr.com/ru/post/1754738/


All Articles