Page 1 of 1

Second Ranorex Thread checking for AUT Exceptions?

Posted: Fri Jul 10, 2009 11:31 pm
by Ciege
Is it possible to have a second thread that its sole purpose is looking for AUT exceptions?

I know I can write a method to check for an exception window, but I don't want to have to call that method every X seconds or minutes or before and after each step in my automation code.

I would like to be able to have a second thread running in the background of my main automation script checking for an AUT Exception window. If one does appear I would like the main script to take a screenshot and halt.

Possible?

This will be using C# in Visual Studio 2008.

Thanks!

Re: Second Ranorex Thread checking for AUT Exceptions?

Posted: Sat Jul 11, 2009 12:41 am
by Ciege
OK, I believe I have figured out creating a second thread to specifically check for an AUT exception window. I can have the second thread put a Report.Screenshot to the log when/if an AUT exception window is found.

Now, I need to figure out how to stop the main thread if the second thread does find an exception window since I would not want the main thread to try and continue testing if there is an AUT exception.

Re: Second Ranorex Thread checking for AUT Exceptions?

Posted: Mon Jul 13, 2009 7:15 pm
by Ciege
So like I said. I've figured a way to create a background thread to check for an exception window in my AUT. However, I just can't seem to figure out how to tell my main thread that there has been an exception and my main thread should clean up and quit. Any ideas?
namespace H_MathRegression_English
{
    public class Worker
    {
        [STAThread]
        public void ThreadTask()
        {
            int i = 0;
            while (i == 0)
            {
                foreach (Ranorex.Form form in Host.Local.Find<Ranorex.Form>("/form[@title='Unhandled Application Thread Exception']"))
                {
                    if (form.Visible && form.Enabled /*..whatever...*/)
                    {
                        //MessageBox.Show("ERROR");
                        Report.Failure("Unhandled Application Thread Exception Detected");
                        Report.Screenshot();
                        //throw new Exception("ERROR");
                        i++;
                    }
                }
                Thread.Sleep(1000);
            }
            Thread.CurrentThread.Abort();
        }
    }

    class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
          <...>
                       // Create the thread object. This does not start the thread.
            Worker workerObject = new Worker();
            Thread workerThread = new Thread(workerObject.ThreadTask);

            // Start the worker thread.
            workerThread.SetApartmentState(ApartmentState.STA);
            workerThread.IsBackground = true;
            workerThread.Start();
          <...>

Re: Second Ranorex Thread checking for AUT Exceptions?

Posted: Tue Jul 14, 2009 9:00 am
by Support Team
hi ciege,
save a reference to the main thread and raise the ThreadAbortException by calling the Thread.Abort() method.

like:
namespace WorkerThreadTest
{
	public class Worker
    {
		Thread mainThread = null;
		
		public Thread MainThread
		{
			set{mainThread = value;}
		}
		
        [STAThread]
        public void ThreadTask()
        {   	
            int i = 0;
            while (i < 4)
            {  
                Thread.Sleep(1000);
                i++;
            }         
   
            if(mainThread!=null)
            	mainThread.Abort("SUTAbort");
        }
    }
	
	
    class Program
    {
        [STAThread]
        public static int Main(string[] args)
        {
            Keyboard.AbortKey = System.Windows.Forms.Keys.Pause;
            int error = 0;

            string logFileName = "Test.rxlog";

            Report.Setup(ReportLevel.Info, logFileName, true);

            try
            {
                Worker workerObject = new Worker();
                workerObject.MainThread = Thread.CurrentThread;
            	Thread workerThread = new Thread(workerObject.ThreadTask);

            	// Start the worker thread.
            	workerThread.SetApartmentState(ApartmentState.STA);
            	workerThread.IsBackground = true;
            	workerThread.Start();
            	
            	// only for testing
            	Thread.Sleep(100000);
            }
            catch (RanorexException e)
            {
                Report.Error(e.ToString());
                Report.Screenshot();
                error = -1;
            }
            catch (ThreadAbortException ex)
            {
            	if(ex.ExceptionState.Equals("SUTAbort"))
            		Report.End();
            	
                Thread.ResetAbort();
            }
            catch (Exception e)
            {
                Report.Error("Unexpected exception occured: " + e.ToString());
                error = -1;
            }
			
            Report.End();
            return error;
        }
    }
}
Regards,
Christian
Ranorex Support Team

Re: Second Ranorex Thread checking for AUT Exceptions?

Posted: Tue Jul 14, 2009 5:33 pm
by Ciege
Excellent, thank you! That does exactly (so far anyway) what I needed it to do!

Re: Second Ranorex Thread checking for AUT Exceptions?

Posted: Wed Jul 15, 2009 3:39 pm
by atom
This is nice...
Just one question, can the worker and background thread use the same ranorex report ?

Re: Second Ranorex Thread checking for AUT Exceptions?

Posted: Wed Jul 15, 2009 3:54 pm
by Ciege
Mine does. Everything in both threads is logged correctly in the main report. Very nice huh?!?

Re: Second Ranorex Thread checking for AUT Exceptions?

Posted: Mon Jul 20, 2009 7:23 pm
by atom
ok cool...
Lets assume we have 2 threads then:
- TestCase thread
- Error dialog thread

How can the Error dialog thread pause the TestCase thread ?
Reason is that in my Error dialog thread, I want to do more than just log the dialog, but attempt to close it
but as one error dialog can lead instantly to another, this can be an iterative process.
So during this process id like to suspend the TestCase thread until all error dialogs are out of the way

Cheers

Re: Second Ranorex Thread checking for AUT Exceptions?

Posted: Mon Jul 20, 2009 7:38 pm
by Ciege
Haven't looked into that very far. My thread is only looking for a specific AUT exception then stops everything since if I get that exception there is something wrong in my AUT and I do not want to continue.

Re: Second Ranorex Thread checking for AUT Exceptions?

Posted: Wed Jul 22, 2009 5:50 pm
by atom
So are the Ranorex types thread safe?
I mean if two threads call say "host.Local.Find(...)" its ok?

Re: Second Ranorex Thread checking for AUT Exceptions?

Posted: Wed Jul 22, 2009 6:12 pm
by Ciege
I'm afraid I cannot answer that authoritatively. Will need support to jump in on this one.

All I can say is that I have not had an issue with it thus far.

Re: Second Ranorex Thread checking for AUT Exceptions?

Posted: Thu Jul 23, 2009 2:00 pm
by atom
Have implemented this slightly differently
Ive used a BackgroundWorker component, as it has the advantage of reporting its progress back to the main thread.

Re: Second Ranorex Thread checking for AUT Exceptions?

Posted: Thu Jul 23, 2009 4:03 pm
by Ciege
Please share your code. Would love to see different variations of how this can be done.

Thanks!

Re: Second Ranorex Thread checking for AUT Exceptions?

Posted: Mon Jul 27, 2009 4:06 pm
by Support Team
atom wrote:So are the Ranorex types thread safe?
I mean if two threads call say "host.Local.Find(...)" its ok?
Yes, in general Ranorex types are thread safe. So, two threads calling Host.Local.Find(...) at the same time are ok.

There may be problems though with elements that internally use COM (like Msaa and Web elements) when you search the elements on one thread and then try to use them on another thread. In that case the first thread that created the elements must still be alive when the elements are accessed by the second thread.

Regards,
Alex
Ranorex Support Team