1

私はクローラーを構築しており、それを行うためにaBotを使用しています。とても素晴らしいシステムです :) 開発中に、aBot プロジェクト自体よりも、クローラーの作成方法に関連する問題を見つけましたが、助けていただければ幸いです。

クローラーを設定するときに、クロールが完了したときに呼び出されるメソッドを指定します。同期オプションと非同期オプションがあります。

        crawler.PageCrawlCompleted += crawler_ProcessPageCrawlCompleted;
        crawler.PageCrawlCompletedAsync += crawler_ProcessPageCrawlCompleted;

古い URL の処理中に別の URL をクロールすることになるため、非同期の URL を使用したいと思います。これは、最後の URL をクロールするまで正常に機能します。最後の 1 つをクロールすると、completeAsync メソッドが呼び出され、クローラーが動作を完了するため、_ProcessPageCrawlComplete メソッドの処理が完全に終了することなく終了し、プログラムが閉じられるため、最後の URL が処理されることを保証できません。

アプリケーションを閉じる前に、この最後のイベントが終了するのを待つ方法はありますか? これは設計上の欠陥ですか?

編集: 言い忘れました: 私はクローラー コードにアクセスできます。私の現在の回避策は次のとおりです。リンクが処理される最後のリンクである場合は、WaitHandle を作成し、完了するまで待ちます。ちょっと雑に聞こえますが…

4

1 に答える 1

5

ManualResetEventは 1 つの解決策になります。

呼び出し方法で:

//Declare the reset event
ManualResetEvent mre = new ManualResetEvent(false);

//Call the async method and subscribe to the event 
crawler.PageCrawlCompletedAsync += crawler_ProcessPageCrawlCompleted;

//The application will wait here until the mre is set.
mre.WaitOne();

イベント ハンドラーで:

private void crawler_ProcessPageCrawlCompleted(...)
{
   ....
   mre.Set();
}

もう 1 つのアプローチは、CountdownEventです。10 ページをクロールする必要があるとします。

CountdownEvent countdown = new CountdownEvent (10);

//Subscribe to the event 
crawler.PageCrawlCompletedAsync += crawler_ProcessPageCrawlCompleted;

//Call 10 time the async method
....

//Wait for all events to complete
countdown.Wait();

ハンドラーで:

private void crawler_ProcessPageCrawlCompleted(...)
{
    ....
   mre.Signal();
}
于 2013-11-06T15:07:35.943 に答える