How to tell if Pause/Abort has been pressed?

Ask general questions here.
AppTester
Posts: 20
Joined: Thu Sep 29, 2011 12:11 am

How to tell if Pause/Abort has been pressed?

Post by AppTester » Mon Mar 12, 2012 6:31 pm

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?

User avatar
Ciege
Ranorex Guru
Posts: 1335
Joined: Thu Oct 16, 2008 6:46 pm
Location: Arizona, USA

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

Post by Ciege » Mon Mar 12, 2012 6:59 pm

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.
If this or any response has helped you, please reply to the thread stating that it worked so other people with a similar issue will know how you fixed your issue!

Ciege...

AppTester
Posts: 20
Joined: Thu Sep 29, 2011 12:11 am

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

Post by AppTester » Mon Mar 12, 2012 10:13 pm

Thank you. Comparing the text of the e object works nicely.

User avatar
sdaly
Posts: 238
Joined: Mon May 10, 2010 11:04 am
Location: Dundee, Scotland

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

Post by sdaly » Tue Mar 13, 2012 9:08 am

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!