1

I need a function that allows me to wait x seconds before doing next thing, and a function that allows me to wait until web pages have loaded before doing next thing. Here is my code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MafiaspilletBot
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            webBrowser1.Navigate("mafiaspillet.no");
            webBrowser1.ScriptErrorsSuppressed = true;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            SendData();
        }
        private void SendData()
        {
            webBrowser1.Document.GetElementsByTagName("input").GetElementsByName("brukernavn")[0].SetAttribute("value", textBox2.Text);
            webBrowser1.Document.GetElementsByTagName("input").GetElementsByName("passord")[0].SetAttribute("value", textBox1.Text);
            webBrowser1.Document.GetElementsByTagName("input").GetElementsByName("login_buton")[0].InvokeMember("click");
            //When the click is done I want it to wait 10 seconds and then I want it to do the DoKrims();
            DoKrims();
 
        }
        private void DoKrims()
        {
            webBrowser1.Navigate("http://mafiaspillet.no/kriminalitet3.php");
            //Here I want it to wait to the page is loaded before completing next
            webBrowser1.Document.GetElementById("submit").InvokeMember("click");
            //Here I want it to wait 3 seconds then load the next page
            webBrowser1.Navigate("http://mafiaspillet.no/kriminalitet3.php");
            //Here I want it to wait to the page is loaded before I completing next
            webBrowser1.Document.GetElementById("submit").InvokeMember("click");
        }
    }
}

As you can see in the code I explained where and what I want it do. Also I've tried System.Threading.Thread.Sleep(10000) but that just make the whole application stop for 10 seconds.

halfer
  • 19,824
  • 17
  • 99
  • 186
Patric Nøis
  • 208
  • 2
  • 8
  • 27
  • Does the webbrowser's `DocumentCompleted` event do what you need in regards to waiting for a webpage to finish loading? – Ulric Oct 02 '15 at 13:50
  • Ive not comt that far yet that ive learnd how to use it, as i started a few hours ago lerning c# – Patric Nøis Oct 02 '15 at 13:54
  • why do you need to wait 10 seconds? – user1666620 Oct 02 '15 at 13:59
  • @user1666620 since when the form is submited on the page it takes like 10 seconds to for the page to get finnished loading. soo i hope there is a way – Patric Nøis Oct 02 '15 at 14:00
  • Why not have javascript functions do an ajax request once the page has rendered? You're asking server side code to determine when the client-side has loaded based on guesswork - why not have the client do the work for you? that way you can avoid placing sleeps of arbitrary durations in your code. – user1666620 Oct 02 '15 at 14:01
  • Yeah but isnt there a way i can set a delay like i click the button once then it does the first 3 things, then it waits 10 second then it does the rest? – Patric Nøis Oct 02 '15 at 14:06
  • You could add a `Timer` with a tick duration of 10000. Then on the `Tick()` event do the other operations. – user1666620 Oct 02 '15 at 14:07
  • @user1666620 could you post an example of how i would do this? – Patric Nøis Oct 02 '15 at 14:08
  • @PatricNøis http://stackoverflow.com/questions/12535722/what-is-the-best-way-to-implement-a-timer http://stackoverflow.com/questions/186084/how-do-you-add-a-timer-to-a-c-sharp-console-application – user1666620 Oct 02 '15 at 14:11
  • @user1666620 i took a look at it but can still not solve how i can use it in my code, i guess c# isnt as easy as every one told me... – Patric Nøis Oct 02 '15 at 14:53

0 Answers0