1

AsyncEnumeratorライブラリを手に入れて、不足している部分がわからない。ライブラリの作成者自身が提供した例を実行しようとしていますが、次の行の「実行」が何をするのかわかりません。

ae.Execute(ProcessAllAndEachOps(ae, urls));

メソッドを参照してください。誰かが私にいくつかの手がかりを与えることができますか?

更新:以下のコードにすでに含まれているいくつかの変更を加えることで、なんとか実行できました。AsyncEnumeratorオブジェクトのExecute()メソッドが廃止されており、何らかのヘルパー関数に置き換える必要があることに気付いたのは、Peterの功績です。

using System;
using System.Collections.Generic;
using System.Net;
using Wintellect.Threading.AsyncProgModel;


public static class AsyncEnumeratorPatterns {

    private static void Execute(AsyncEnumerator ae, IEnumerator<Int32> enumerator){
                    ae.EndExecute(ae.BeginExecute(enumerator, null));
    }

      public static void Main() {
        String[] urls = new String[] { 
          "http://Wintellect.com/", 
          "http://1.1.1.1/",   // Demonstrates error recovery
          "http://www.Devscovery.com/" 
        };

        // Demonstrate process
        AsyncEnumerator ae = new AsyncEnumerator();
        Execute(ae, ProcessAllAndEachOps(ae, urls));
      }

      private static IEnumerator<Int32> ProcessAllAndEachOps(
           AsyncEnumerator ae, String[] urls) {
        Int32 numOps = urls.Length;

        // Issue all the asynchronous operation(s) so they run concurrently
        for (Int32 n = 0; n < numOps; n++) {
          WebRequest wr = WebRequest.Create(urls[n]);
          wr.BeginGetResponse(ae.End(), wr);
        }

        // Have AsyncEnumerator wait until ALL operations complete
        yield return numOps;

        Console.WriteLine("All the operations completed:");
        for (Int32 n = 0; n < numOps; n++) {
          ProcessCompletedWebRequest(ae.DequeueAsyncResult());
        }

        Console.WriteLine(); // *** Blank line between demos ***

        // Issue all the asynchronous operation(s) so they run concurrently
        for (Int32 n = 0; n < numOps; n++) {
          WebRequest wr = WebRequest.Create(urls[n]);
          wr.BeginGetResponse(ae.End(), wr);
        }

        for (Int32 n = 0; n < numOps; n++) {
          // Have AsyncEnumerator wait until EACH operation completes
          yield return 1;

          Console.WriteLine("An operation completed:");
          ProcessCompletedWebRequest(ae.DequeueAsyncResult());
        }
      }

      private static void ProcessCompletedWebRequest(IAsyncResult ar) {
        WebRequest wr = (WebRequest)ar.AsyncState;
        try {
          Console.Write("   Uri=" + wr.RequestUri + "    ");
          using (WebResponse response = wr.EndGetResponse(ar)) {
            Console.WriteLine("ContentLength=" + response.ContentLength);
          }
        }
        catch (WebException e) {
          Console.WriteLine("WebException=" + e.Message);
        }
    }
}
4

1 に答える 1

0

他の人が述べているように、Executeメソッドはしばらく前に非推奨になり、現在は完全になくなっており、MSDNの例はかなり古いものです。

That method slightly defeated the point of the AsyncEnumerator class, as the thread making the call will run the iterator until the first yield and then block until the iterator has finished processing.

So its maybe more clear to replace it with:

var asyncResult = ae.BeginExecute(ProcessAllAndEachOps(ae, urls), null);
asyncResult.AsyncWaitHandle.WaitOne();
ae.EndExecute(asyncResult);

Which looks a little bit more like making regular APM calls from within an AsyncEnumerator iterator and makes the blocking more explicit.

于 2012-05-29T23:30:54.027 に答える