Second Ranorex Thread checking for AUT Exceptions?

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

Second Ranorex Thread checking for AUT Exceptions?

Post by Ciege » Fri Jul 10, 2009 11:31 pm

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!
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...

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

Re: Second Ranorex Thread checking for AUT Exceptions?

Post by Ciege » Sat Jul 11, 2009 12:41 am

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.
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...

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

Re: Second Ranorex Thread checking for AUT Exceptions?

Post by Ciege » Mon Jul 13, 2009 7:15 pm

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();
          <...>
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...

User avatar
Support Team
Site Admin
Site Admin
Posts: 11709
Joined: Fri Jul 07, 2006 4:30 pm
Location: Graz, Austria

Re: Second Ranorex Thread checking for AUT Exceptions?

Post by Support Team » Tue Jul 14, 2009 9:00 am

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
.
Image

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

Re: Second Ranorex Thread checking for AUT Exceptions?

Post by Ciege » Tue Jul 14, 2009 5:33 pm

Excellent, thank you! That does exactly (so far anyway) what I needed it to do!
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...

atom
Posts: 357
Joined: Sun Dec 07, 2008 11:14 pm
Location: Dublin, Ireland

Re: Second Ranorex Thread checking for AUT Exceptions?

Post by atom » Wed Jul 15, 2009 3:39 pm

This is nice...
Just one question, can the worker and background thread use the same ranorex report ?

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

Re: Second Ranorex Thread checking for AUT Exceptions?

Post by Ciege » Wed Jul 15, 2009 3:54 pm

Mine does. Everything in both threads is logged correctly in the main report. Very nice huh?!?
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...

atom
Posts: 357
Joined: Sun Dec 07, 2008 11:14 pm
Location: Dublin, Ireland

Re: Second Ranorex Thread checking for AUT Exceptions?

Post by atom » Mon Jul 20, 2009 7:23 pm

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

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

Re: Second Ranorex Thread checking for AUT Exceptions?

Post by Ciege » Mon Jul 20, 2009 7:38 pm

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.
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...

atom
Posts: 357
Joined: Sun Dec 07, 2008 11:14 pm
Location: Dublin, Ireland

Re: Second Ranorex Thread checking for AUT Exceptions?

Post by atom » Wed Jul 22, 2009 5:50 pm

So are the Ranorex types thread safe?
I mean if two threads call say "host.Local.Find(...)" its ok?

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

Re: Second Ranorex Thread checking for AUT Exceptions?

Post by Ciege » Wed Jul 22, 2009 6:12 pm

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.
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...

atom
Posts: 357
Joined: Sun Dec 07, 2008 11:14 pm
Location: Dublin, Ireland

Re: Second Ranorex Thread checking for AUT Exceptions?

Post by atom » Thu Jul 23, 2009 2:00 pm

Have implemented this slightly differently
Ive used a BackgroundWorker component, as it has the advantage of reporting its progress back to the main thread.

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

Re: Second Ranorex Thread checking for AUT Exceptions?

Post by Ciege » Thu Jul 23, 2009 4:03 pm

Please share your code. Would love to see different variations of how this can be done.

Thanks!
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...

User avatar
Support Team
Site Admin
Site Admin
Posts: 11709
Joined: Fri Jul 07, 2006 4:30 pm
Location: Graz, Austria

Re: Second Ranorex Thread checking for AUT Exceptions?

Post by Support Team » Mon Jul 27, 2009 4:06 pm

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
.
Image