Consider this: You have a .NET routine that takes some time to complete. While this routine runs, you decide you have had enough of the waiting and would like to stop this routine and return to the command prompt when the user presses the ESCAPE key. Is there a way to do this?
The HostApplicationServices.UserBreak method allows the application to check the escape key and take the appropriate action.
The code below shows this approach. A long process is started in a simple loop that calls thread sleep. On each iteration the code checks the current HostApplicationServices to see if the escape key was pressed. If it was pressed the code throws an exception where the program can finish gracefully.
public static void myLongFunction()
//show the progress of the function
ProgressMeter pm = new ProgressMeter();
//start a long process
for (int i = 0; i < 100; i++)
//did user press ESCAPE?
throw new Autodesk.AutoCAD.Runtime.Exception(
ErrorStatus.UserBreak, "ESCAPE pressed");
//update progress bar
//delay 10 miliseconds
catch (System.Exception ex)