1

I am trying to login to my website automatically using webBrowser control in my C# application. The webpage checks the time required to fill and submit the login form. If the time taken is less than 2 seconds, then it shows an 'error page' saying 'Robots not allowed'.

Now my application is also getting that 'error page' just because the login form is getting filled within 2 seconds. How can I add a delay before firing the 'InvokeMember("click")' so that the time calculated by the webpage to fill the form be more than 2 seconds.

Here is my code

HtmlElement ele = WebBrowser1.Document.GetElementById("username");
if (ele != null)
{     
    ele.InnerText = "myUsrname";
}
ele = webBrowser1.Document.GetElementById("password");
if (ele != null)
{
    ele.InnerText = "myPasswrd";
}
ele = webBrowser1.Document.GetElementById("submit");
if (ele != null)
{
    // I want to add a delay of 3 seconds here
    ele.InvokeMember("click");
}

Note : I used "Task.Delay(3000);" but it doesn't seems to work.

Edit : This is what I am using now and is working for me.

async void webBrowser1_DocumentCompleted_1(object sender, WebBrowserDocumentCompletedEventArgs e)
{
......//my code
 await Task.Delay(3000);// this is where I wanted to put a delay
....
}

But I would like to, Is this a correct way to use it ?

Thundrstorm
  • 181
  • 3
  • 16

2 Answers2

0

You could use this:

int milliseconds = 2000;
Thread.Sleep(milliseconds)  

Greetings,
ST

Sebastian Tam
  • 119
  • 2
  • 8
  • Thanks. I referred [this](http://stackoverflow.com/questions/5449956/how-to-add-a-delay-for-a-2-or-3-seconds) . and found the comments on the same answer. Please guide, whether I should use this or not in my case. – Thundrstorm Sep 24 '15 at 11:21
0

If you want to don't freeze UI when waiting, Consider this sample:

private async void button1_Click(object sender, EventArgs e)
{
    await Task.Run(async () =>
    {
        await Task.Delay(3000);
        MessageBox.Show("1");
        button1.Invoke(new Action(() => { this.button1.Text = "1"; }));
    });
    MessageBox.Show("2");
    button1.Invoke(new Action(() => { this.button1.Text = "2"; }));
}

The sample is self describing.

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
  • Thanks, I added the keyword 'async' in my function before 'void' and simply added the line 'await Task.Delay(3000);' which is now working. Thanks. – Thundrstorm Sep 24 '15 at 13:03