it's a .NET non GUI application. It is specified as a console application, but the console is not really used. what the application does is GUI testing of other applications
You would have to have your main thread waiting on Console.Read()
(or Console.ReadKey()
) all the time and do the actual work on another thread. When the main thread detects a keypress it needs to somehow notify the other thread to pause or resume (eg. using a WaitHandle
).
If the worker thread is doing something really simple and it's safe to suspend it at any time then you may be able to just suspend/resume the thread itself rather than doing any notifications.
It'd be nice if the Console class supported other events wouldn't it? :)
To do work and have a responsive program, I imagine you could have a loop constantly polling the KeyAvailable
property for any input. Then probably have another separate thread doing the actual work. If a key becomes available, you can force the worker thread to sleep (pause) or wake (continue). Otherwise if there is none, you could update the console with the status. For a single threaded system, you might want to throw in some yields in there and some timers to prempively yield your worker thread.
Others have described how you'd start/stop the problem execution ... for the actual key trapping, I'd suggest registering a global hotkey - but, you'd need to have a Windows form handle available to you, so I'd also suggest either launching your command-line utility from a GUI app, or simply including the functionality in a GUI app.
Is this kind of change possible?
EDIT: Christian Liensberger has made what looks like an excellent wrapper for this on his blog.