A
A
Alexey Nazarenko2019-01-21 00:44:48
Automation
Alexey Nazarenko, 2019-01-21 00:44:48

How to implement timeout in C#?

Good day to all readers!
Please help me implement timeout in C#.
I am engaged in the task of automating a desktop application using the Winium.Cruciatus framework and I need to set a timeout to search for an application element, that is, the search for an element must be performed within a specified time and, if the element is not found during this time, an error should be generated, which I then I can process.
Here is the code for finding the element on which to set the timeout:

var OpenShift = CashierWindow.FindElementByname("Да");

{

try
    {

OpenShift.Click();
CashierWindow.FindElementByUid("6").Click();
CashierWindow.FindElementByUid("6").Click();

    }

catch

    {

Console.WriteLine("...");

    }

}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Nazarenko, 2019-01-23
@Cunctator

Found a solution by launching an asynchronous operation and setting the function execution interval using the CruciatusFactory.Settings.SearchTimeout property

using System;
using System.Threading.Tasks;
using Winium.Cruciatus.Core;
using Winium.Cruciatus.Elements;
using Winium.Cruciatus;


static class IfExistsClass
{
    public static CruciatusElement GetIfExists(By element, CruciatusElement root, double timeout = 90000)

    {

        var StartDateTime = DateTime.Now;

        CruciatusFactory.Settings.SearchTimeout = (int)timeout;

        while ((DateTime.Now - StartDateTime).TotalMilliseconds < timeout) ;

        {
            var FindElement = Task<CruciatusElement>.Run(() =>


            {
                var elementFound = root.FindElement(element);
                return elementFound;


            });

            if (FindElement.Wait(TimeSpan.FromMilliseconds(30000)))
                return FindElement.Result;

            else

                Console.WriteLine("Timeout поиска элемента");


        }

        return null;

    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question