Page 1 of 1

Concurrent Finds

Posted: Fri Feb 18, 2011 12:28 pm
by sdaly
Hi

I use Threadpools for searching for dialogues which may or may not appear in the background. This all seems to work fine.

If however there were two buttons on dialogues being searched for in two seperate threads and Ranorex finds them both at the exact same time, how does it dispatch the clicks. Does Ranorex handle and control this or should I be handling this myself using a Mutex?

Thanks
Scott

Re: Concurrent Finds

Posted: Fri Feb 18, 2011 2:24 pm
by Support Team
Hi Scott,

Concurrent searching should be no problem; Find() might encounter an internal synchronization point at some point, though, so not everything is really concurrent, depending on the technology.

Any input actions (Mouse move & clicks, keyboard) must be synchronized. Just use:
element = Find(...)

lock(sharedObject)
{
   element.Click();
}
Michael
Ranorex Team

Re: Concurrent Finds

Posted: Fri Feb 18, 2011 3:10 pm
by sdaly
Hi Michael

Thanks for the info :)
I was wondering about this as we have a dialogue that sometimes appears which you have to click Yes which then you need to click Proceed. Both these buttons are being searched at the same time in the background but sometimes Proceed is not clicked. I'm a little wary that maybe the click is being dispatched too quickly after the first one found. I've chucked a Mutex and delay(it seemed the easiest way to do it) into my TryClick method which seems to work a treat.

Public Class ThreadData
        Public _rxPath As String
        Public _timeout As Integer

        Public Sub New(ByVal rxPath As String, ByVal timeout As Integer)
            _rxPath = rxPath
            _timeout = timeout
        End Sub
    End Class

    Public Sub TryClick(ByVal strRanorexPath As String, ByVal intTimeout As Integer, Optional ByVal singleThread As Boolean = False)
        Dim data As New ThreadData(strRanorexPath, intTimeout)
        If singleThread Then
            TryClickMain(data)
        Else
            ThreadPool.QueueUserWorkItem(New WaitCallback(AddressOf TryClickMain), data)
        End If
    End Sub

    Private _Mutex As New Mutex

    Private Sub TryClickMain(ByVal data As Object)
        Dim _data As ThreadData = CType(data, ThreadData)
        Dim Item As Ranorex.Unknown = Nothing
        If Host.Local.TryFindSingle(_data._rxPath, _data._timeout, Item) = True Then
            _Mutex.WaitOne()
            Item.EnsureVisible()
            Item.Click()
            Thread.Sleep(1000)
            _Mutex.ReleaseMutex()
        End If
    End Sub