views:

136

answers:

3

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

+1  A: 

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.

Evgeny
but this would only work if the console has focus, am I right? but in my application this is difficult since the tested GUI usually has the focus (actually almost all of the time)
Louis Rhys
Yes, you're right - if doesn't have focus then you'd have to register a global hotkey, as overslacked says.
Evgeny
A: 

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.

Jeff M
KeyAvailable will not work when the console does not have focus when the key is pressed, will it?
Louis Rhys
The program will be running whether it is focused (active) or not.It's a boolean and remains false until a key is pressed.
Jeff M
but would the press be detected only when the console is focused?
Louis Rhys
Yes, that's the point of having the worker thread. The main thread and the worker thread would be running simultaneously effectively.
Jeff M
+2  A: 

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.

overslacked