2016-09-18 42 views
0

所以我一直在努力解決這個問題很長一段時間了。我試圖找出如何啓用計時器,並在網頁加載完成後將間隔設置爲1000。C#等待瀏覽器芬蘭語加載然後啓動一個計時器

這裏是代碼我已經試過到目前爲止:

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 whit interval 1000 when the website is finnished 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"); 
     } 
    } 
} 

所以我的問題是我如何可以啓用定時器,當頁面完成加載定時器的時間間隔設置爲1000?

+2

您處理[WebBrowser.DocumentCompleted(https://msdn.microsoft.com/en-us/library/system.windows.forms。 webbrowser.documentcompleted(v = vs.110)的.aspx)? – GSerg

回答

4

您已經知道如何啓用計時器。網頁瀏覽器有一個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; 
} 
+0

換句話說,我們必須訂閱事件'DocumentCompleted'確保我們可以操縱'WebBrowser' 'webBrowser1.DocumentCompleted + =(對象發件人,WebBrowserDocumentCompletedEventArgs E)=> { System.Diagnostics程序。 Debug.WriteLine(「文檔已經加載,我現在可以在它上面進行操作!」); };' –

+0

To - 托馬斯韋勒:爲什麼退訂已完成,有什麼好處'webBrowser1.DocumentCompleted - = DocumentCompleted;'? –

+2

@LJ:由於我不知道接下來做了什麼,我退訂了該活動。這樣可以在其他地方導航而無需在加載下一頁時啓用定時器。 –

相關問題