Concurrent Finds

Class library usage, coding and language questions.
User avatar
sdaly
Posts: 238
Joined: Mon May 10, 2010 11:04 am
Location: Dundee, Scotland

Concurrent Finds

Post by sdaly » Fri Feb 18, 2011 12:28 pm

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

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

Re: Concurrent Finds

Post by Support Team » Fri Feb 18, 2011 2:24 pm

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

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

Re: Concurrent Finds

Post by sdaly » Fri Feb 18, 2011 3:10 pm

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