views:

93

answers:

3

what i want to do is have a background application which by itself or its modules run only when a certain key is pressed. Like when i press F10 some code that i have written executes and then quits or goes to sleep then again when i press F10 i executes again and goes to sleep but in between keystrokes it does not takes any processor time. How can i do it

For example a thread that executes its statements and then

thread.Sleep( "till i press F10" );

well this is an imaginary function. Any help from the real world :)

C# + Winforms

+1  A: 

You can take a look here: http://www.codeproject.com/KB/cs/CSLLKeyboardHook.aspx

John Fisher
yes a piece of code, but doesn't clears the concepts along with it but hey thanks anyways :)
Junaid Saeed
Was this a threading question, a keyboard event question, or both?
John Fisher
A: 

Simplest is probably SetWindowsHookEx with the WH_KEYBOARD or WH_KEYBOARD_LL hook type. Calling it from C# may be a bit obnoxious, but it's possible.

David Seiler
A: 

why dont you try console.ReadKey which will read the key and immediately perform some operation n as soon as task gets finished u call thread.Sleep()

SWATI