c#等待x秒并等待页面加载

时间:2015-10-02 13:45:03

标签: c# load webbrowser-control wait

我需要一个允许我在做下一件事之前等待x秒的函数,以及一个允许我等到网页加载后再做下一件事的函数。这是我的代码:

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 seonds then load the next page
            webBrowser1.Navigate("http://mafiaspillet.no/kriminalitet3.php");
            //Here i want it to wait to the page is lodaed before i completing next
            webBrowser1.Document.GetElementById("submit").InvokeMember("click");
        }
    }
}

正如你在代码中看到的那样,我解释了我想要它做什么和做什么。希望这里的一些人可以帮我解决这个问题:)我也试过System.Threading.Thread.Sleep(10000);但这只会使整个应用程序停止10秒钟。

0 个答案:

没有答案