Generate a wait in browser with C #

2

I am trying to automate a web page using the gecko engine, it is similar to the webBrowser that has Visual Studio incorporated. I'm using the event

navegador.DocumentCompleted += (senderx, ex)=>

To wait for the page to be completed, I am doing well with everything but an input that sometimes fills it and sometimes passes it by, I read it in the following way

Gecko.DOM.GeckoInputElement input = (Gecko.DOM.GeckoInputElement)document.GetElementsByName("txtorderNo")[0];
input.SetAttribute("value", "aKl8_Sbl");
input.Value = "aKl8_Sbl";

To avoid being skipped I want to pause, the issue is that I'm having trouble with that, poor things and nothing works for me

  • The IsBusy and IsAjaxBusy methods always return false
  • The "System.Threading.Thread.Sleep" does not work for me because it slows down the browser load so it's like nothing
  • Using this code makes me wait but I get the following error.

      

    {"Specified argument was out of the range of valid values. \ r \ nParameter name: index"}

    public void Esperar(int tiempo)
    {
        DateTime reloj = DateTime.Now;
        reloj = reloj.AddSeconds(tiempo);
    
        while (reloj > DateTime.Now)
        {
            Application.DoEvents();
        }
    }
    
  • The other two I use are these but they do not work for me either

        async Task Delay()
        {
            await Task.Delay(100000);
    
        }
    
        public void Wait(double seconds)
        {
            Timer timer = new Timer();
            timer.Interval = (int)(seconds * 1000);
            timer.Tick += (s, o) =>
            {
                timer.Enabled = false;
                timer.Dispose();
            };
            timer.Enabled = true;
        }
    

If anyone has an answer, I hope you can help me, regards

    
asked by Alejandro Ricotti 25.07.2016 в 16:51
source

2 answers

1

As indicated by this answer you could make a async Task quite similar to what you already have:

async Task PutTaskDelay()
{
    await Task.Delay(5000);
} 

private async void btnTaskDelay_Click(object sender, EventArgs e)
{
    await PutTaskDelay();
    MessageBox.Show("I am back");
}

Notice that the function is called with a await and that the function that calls it must be async

    
answered by 25.07.2016 / 17:34
source
1

I think that the code you comment fails because it compares with all the time values of the DateTime that arrive up to milliseconds, although, I do not understand well because it occurs, or what the out-of-range offset is. If you limit the comparison to the seconds, since the value of the wait is in seconds, it does not fail.

    public void Esperar(int tiempo)
    {
        DateTime reloj = DateTime.Now;
        reloj = reloj.AddSeconds(tiempo);
        while (reloj.Second > DateTime.Now.Second)
        {
            Application.DoEvents();
        }
    }

- Note: You told me yourself that the error is in the wait method. And what I come to refer, is that in the while you had While (clock> DateTime.Now), which does not compare a second with the current second, but one millisecond, with the current millisecond ... while the delay, it deals occupy X seconds (and not milliseconds). On the other hand, I do not think that Application.DoEvents (); it is for nothing the origin of the error, because what that does, is to review and update the control of the other events, avoiding that there are not controlled events, or that the interface of the window is not updated correctly for this reason; That's why I think that Application.DoEvent (); It is very convenient for the assignment you comment to be completed properly, especially if you have noticed that it was assigned when using that method. There are errors that occur, because a thread of process in the application, is accelerated more than the update of control events, and, checking the other events, the error can be avoided.

    
answered by 25.07.2016 в 18:00