Search code examples
c#debuggingvbscriptkeypress

How to suspend execution and wait for a key-press in a win application?


I am implementing a debugger using c#(working on VS2012 -- .Net 4.5), it should work as below: (This is a vbscript debugger which is using msscript.ocx control)

On the line with breakpoint it should wait for the {F5} key and upon having the {F5} key it should move to next code line.

Now the problem is that in the debug method(This method is called upon hitting breakpoint) keeps on moving in the loop checking for the static variable set as true ( The key-press event on the control sets this static variable as true).

The application goes un-responsive and i have to stop it.

Here goes the code for it:

Following code is implemented at the KeyPress event of a TextBox: Whenever it receive a {F5} key it sets true in a static variable.

static bool dVar;
private void fctb_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.F5)
{
    //Setting the static variable true when the control recieves a {F5} key
    dVar = true;
}
}

Now Upon hitting the breakpoint following function is called

    public void DebugIT()
    {
        dVar=false

            //Waits for the  {F5} key press by checking for the static variable 
           //The Application goes Un-Responsive on this Loop and stops accepting keys
            while (dVar!=true)
            {
                System.Threading.Thread.Sleep(1000); 
            }           


        }



    }

The issue here is that when it enter the while loop it stops accepting the key-presses and goes unresponsive.

Need a way that suspends the code execution till the time it receives a desired key-press.

Or

Can we have a separate thread which checks for the {F5} key press and does not make the application un-Responsive.

Can anyone please help?


Solution

  • You need to add DoEvents into your while loop. See the example from MSDN