简体   繁体   中英

Is There a way to capture a keydown event in an unresponsive form

I have a form and it has a time consuming method that runs in it. I want to cancel that method with a keypress while it's running, however due to some limitations, i can not convert that method to a task or anything async.

The scenario is, user presses a button, the button calls that method (lets say it runs a while(x > y) ) which has takes some, and if the user presses the esc key, i want to break that method.

How can I capture the keypress of an unresponsive form? Since while method blocks the form, keypress methods of the forms are unresponsive.

You can try calling Application.DoEvents from your time consuming method, but that will process also all kinds of messages, including dangerous ones like attempts to close the form, and so on.

Much better if you can rework your code and place our long-running method in a BackgroundWorker .

As you said, it is unresponsive and will be unable to capture input. Instead you can implement a background thread do your job there thus you can cancel thread as you want

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM