Arun Prakash
Arun Prakash

Reputation: 121

C# await Task.Delay Not working as intended with WinForms/Selenium

I am doing a test with a website using Selenium and Winforms. i have to use Delays here and there quite often. i still can't understand what might be a problem. and i am not that good with whole " deadlock " thing but i checked few answers here and the code doesn't seem to a deadlock one.

so basically, this is the function internal async Task HandleInput() the use of it is the loop through each line of a text file then enter it to a website. the loop, every logic is in place correctly, and it was working until yesterday and today when i started VS and ran a check on the app, it just gets struck right on that line where the await Task.Delay(1000); is written. on the other hand, Task.Delay(1000).Wait(); works fine.

This is how i call the Task Function from the main HandleInput() Function.

Logger.LogGenericText("starting process...");                    
                await SignInToSite(DriverChrome, Wait, id, pass);

here is the SignInToSite() Function:

Function Head: public async Task SignInToSite(IWebDriver DriverChrome, WebDriverWait Wait, string id, string pass)

try
        {
            Logger.LogGenericText("Acessing Login URL...");
            DriverChrome.Navigate().GoToUrl("xxxxxxxxxxxxxxxxxxxxx");
            await Task.Delay(1000); //entire program goes to standstill in here, UI isnt struck. 
            Logger.LogGenericText("Passing id and Password to respective fields...");
            Wait.Until(d => d.FindElement(By.Name("username"))).SendKeys(steamId);
            await Task.Delay(1000);
            Wait.Until(d => d.FindElement(By.Name("password"))).SendKeys(pass);                
            Wait.Until(d => d.FindElement(By.Id("UserLogin"))).Click();
            await Task.Delay(3000);
        }
        catch (Exception ex)
        {
            Logger.LogGenericText(ex.ToString());
            return;
        }

I tried the compiled program on another PC, No fix. would be glad if anyone could point me to the right direction.

Upvotes: 1

Views: 1024

Answers (1)

Arun Prakash
Arun Prakash

Reputation: 121

Ok so, seems like it was some sort of deadlock or something. However, i managed to fix it by using ConfigureAwait(false) after every await statement. i used the ConfigureAwait Checker From Nuget to get the things done fast.

Upvotes: 1

Related Questions