Page 1 of 1

How to tell if Pause/Abort has been pressed?

Posted: Mon Mar 12, 2012 6:31 pm
by AppTester
Lets say I have some code that keeps clicking a button all day, until 2:00 pm, when the button disappears and Ranorex can stop:

Code: Select all

try
			{
				keepRunning:
				
				// while window visible, keep doing the following
				while (repo.FormAdd_Markets_to_Market_Da.ButtonDone.Visible)
				{
					repo.FormICE_Formula_Trader_Light.Container_userDataArea.EnsureVisible();
					repo.FormICE_Formula_Trader_Light.Container_userDataArea.Click();
				}
			}
			catch // window gone or Pause key has been pressed
			{
				if ("2:00 pm")
				{
					// do some things and then let program close
				}
				else
				{
					// re-open window with button and keep clicking
					// ... code removed 
					goto keepRunning;
				}
			}
This works fine for me, except for one case. When the user clicks the Pause key, an exception is thrown in the try block and when it is not 2 pm, and Ranorex tries to re-open the screen with the button before stopping.

The only way I can think around this is to check if the Pause key was clicked in the catch, but how can I do this? Is it possible to programmatically determine if the Pause key was pressed within Ranorex?

Re: How to tell if Pause/Abort has been pressed?

Posted: Mon Mar 12, 2012 6:59 pm
by Ciege
Your catch has 2 possible exits.
1) It is 2:00 PM, and it should exit
2) It is NOT 2:00 PM and it should continue

You can add a statement to evaluate the exception such as:
If e.text = "whatever exception is thrown with a pause press" then exit

You can determine the text of the exception by reporting what e.text equals from within the catch block.

Re: How to tell if Pause/Abort has been pressed?

Posted: Mon Mar 12, 2012 10:13 pm
by AppTester
Thank you. Comparing the text of the e object works nicely.

Re: How to tell if Pause/Abort has been pressed?

Posted: Tue Mar 13, 2012 9:08 am
by sdaly
You may also catch the relevant exceptions like so -

Code: Select all

try {
				
				//do something
				
			}catch (ElementNotFoundException) {
				
				Console.WriteLine("Element not found");
				
			}catch(ThreadAbortException){
				
				Console.WriteLine("Aborted");
				
			}

...or catch the exception and check the type...

Code: Select all

			try {
				
				Keyboard.AbortKey = Keys.Escape;
				Console.ReadLine();
				
			}catch (Exception ex) {
				
				if(ex is ElementNotFoundException){
					Console.WriteLine("Element not found.");
				}
				
				if(ex is ThreadAbortException){
					Console.WriteLine("Thread abort");
				}
				
			}
Many ways to skin a cat!