在c#中,等待浏览器完成加载,然后启动计时器
本文关键字:然后 加载 启动 计时器 浏览器 等待 | 更新日期: 2023-09-27 18:07:32
我想知道如何启用计时器,并将其间隔设置为1000时,网页完成加载。
下面是我到目前为止尝试过的代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
webBrowser1.Navigate("website.com");
webBrowser1.ScriptErrorsSuppressed = true;
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
webBrowser1.Document.GetElementsByTagName("input").GetElementsByName("brukernavn")[0].SetAttribute("value", textBox1.Text);
webBrowser1.Document.GetElementsByTagName("input").GetElementsByName("passord")[0].SetAttribute("value", textBox2.Text);
webBrowser1.Document.GetElementsByTagName("input").GetElementsByName("login_buton")[0].InvokeMember("click");
timer1.Enabled = true;
timer1.Interval = 7000;
}
private void timer1_Tick(object sender, EventArgs e)
{
webBrowser1.Navigate("website.com/mygambling.php");
//Here I need a code to enable timer2 with interval 1000 when the website is finished loading
}
private void timer2_Tick(object sender, EventArgs e)
{
webBrowser1.Document.GetElementsByTagName("input").GetElementsByName("bet")[0].SetAttribute("value", 250);
webBrowser1.Document.GetElementsByTagName("input").GetElementsByName("start")[0].InvokeMember("click");
}
}
}
所以我的问题是我如何启用timer2和设置timer2间隔1000时,页面完成加载?
您已经知道如何启用计时器。浏览器有一个DocumentCompleted事件,你可以订阅它。这将帮助您确定页面加载何时完成:
private void timer1_Tick(object sender, EventArgs e)
{
webBrowser1.Navigate("website.com/mygambling.php");
webBrowser1.DocumentCompleted += DocumentCompleted;
}
private void DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
webBrowser1.DocumentCompleted -= DocumentCompleted;
timer2.Enabled = true;
}