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!
Second Ranorex Thread checking for AUT Exceptions?
Second Ranorex Thread checking for AUT Exceptions?
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...
Ciege...
Re: Second Ranorex Thread checking for AUT Exceptions?
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.
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...
Ciege...
Re: Second Ranorex Thread checking for AUT Exceptions?
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...
Ciege...
- Support Team
- Site Admin
- Posts: 12145
- Joined: Fri Jul 07, 2006 4:30 pm
- Location: Houston, Texas, USA
- Contact:
Re: Second Ranorex Thread checking for AUT Exceptions?
hi ciege,
save a reference to the main thread and raise the ThreadAbortException by calling the Thread.Abort() method.
like:
Christian
Ranorex Support Team
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?
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...
Ciege...
Re: Second Ranorex Thread checking for AUT Exceptions?
This is nice...
Just one question, can the worker and background thread use the same ranorex report ?
Just one question, can the worker and background thread use the same ranorex report ?
Re: Second Ranorex Thread checking for AUT Exceptions?
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...
Ciege...
Re: Second Ranorex Thread checking for AUT Exceptions?
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
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?
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...
Ciege...
Re: Second Ranorex Thread checking for AUT Exceptions?
So are the Ranorex types thread safe?
I mean if two threads call say "host.Local.Find(...)" its ok?
I mean if two threads call say "host.Local.Find(...)" its ok?
Re: Second Ranorex Thread checking for AUT Exceptions?
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.
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...
Ciege...
Re: Second Ranorex Thread checking for AUT Exceptions?
Have implemented this slightly differently
Ive used a BackgroundWorker component, as it has the advantage of reporting its progress back to the main thread.
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?
Please share your code. Would love to see different variations of how this can be done.
Thanks!
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...
Ciege...
- Support Team
- Site Admin
- Posts: 12145
- Joined: Fri Jul 07, 2006 4:30 pm
- Location: Houston, Texas, USA
- Contact:
Re: Second Ranorex Thread checking for AUT Exceptions?
Yes, in general Ranorex types are thread safe. So, two threads calling Host.Local.Find(...) at the same time are ok.atom wrote:So are the Ranorex types thread safe?
I mean if two threads call say "host.Local.Find(...)" its 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