0

WebBrowserオブジェクトを学習するための簡単な自動サーフィンアプリを作成しています。

数秒ごとにネットサーフィンしている23のURLのリストがあります。

アプリはシンプルです。フォーラムにアクセスし、フォームを開いて新しいメッセージを(送信せずに)追加し、リストの最後に到達するまで続行します。

私の問題は、コードforumAction.FillOutFormIn(webBrowser1.Document);が間違ったサイトで実行されることです。

これは、ドキュメントの準備ができていないために発生したと思います。

では、ドキュメントの準備ができるまで、タイマーがコードを実行するのを停止する方法はありますか?

TIMER TICK関数は次のとおりです。

//I start is in 21 for faster testing.
int timesToRun = 21;
    private void Time_Tick(object sender, EventArgs e)
    {

            Console.WriteLine(timesToRun.ToString());
            string currentSite = siteList.GetSiteFromIndex(timesToRun);

            webBrowser1.Document.Window.Navigate(currentSite);

            //I think I need to wait here until the document is ready

            //this line of code doesn't run on timeToRun = 22
            forumAction.FillOutFormIn(webBrowser1.Document);

            Console.WriteLine(webBrowser1.Url);
            timerLabel1.Text = siteList.SiteLists.Count + ">" + timesToRun + "";

            if (timesToRun >= siteList.SiteLists.Count - 1)
            {
                enableControls(true);
                timesToRun = 0;
                timer.Stop();
                Console.WriteLine("done");

            }

            timesToRun++;          
    }

(私の英語でごめんなさい)

4

3 に答える 3

1

こんな感じでイベント追加

You could disable your timer in your Time_tick function, 

timer1.Enabled = false;

次に、ドキュメント完了イベントで再度有効にします。

private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    if(timesToRun > 0) 
    { 
        timer1.Enabled = true;
    }
}
于 2012-08-24T13:20:00.177 に答える
1

コントロールのDocumentCompletedイベントを単純にコーディングできます。

これにより、ページが読み込まれたときにタイマーを再起動できます。

webBrowser1.Navigated += WebBrowser_DocumentCompleted;
timesToRun = 22;

private void Time_Tick(object sender, EventArgs e)
{
    timer.stop();
    webBrowser1.Document.Window.Navigate(url);
}

void WebBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    timesToRun--;
    if(timesToRun > 0)
    {
        timer.Start();
    }
}
于 2012-08-24T13:20:48.570 に答える