2016-12-05 52 views
-3

請注意,它必須只有.NET 4.0代碼!允許使用async/await關鍵字和Task類。但是在.NET 4.0中沒有像WhenAny()這樣的方法。c#.net 4.0遏制模式異步下載

我有個任務將程序功能,將:

///Return the content of required uris. 
/// Method has to use the asynchronous way. 
/// maxConcurrentStreams parameter should control the maximum of 
/// concurrent streams that are running at the same time (throttling). 

/// <param name="uris">Sequence of required uri</param> 
/// <param name="maxConcurrentStreams">Max count of concurrent request streams</param> 
/// <returns>The sequence of downloaded url content</returns> 
public static IEnumerable<string> GetUrlContentAsync(this IEnumerable<Uri> uris, int maxConcurrentStreams) 
{ 
.... ??? 
} 

所以。問題是uri下載字符串數據(只是html),同時最多併發strem(節流)。

+0

「.NET 4.0 only」是什麼意思?不再支持.NET 4.0,而C#6.0與4.5一起推出。最早支持的.NET版本是4.5.2。 TLS 1.2僅支持4.5.2及更高版本,因此TCP相關的任何內容至少應以4.5.2爲目標。 –

+0

你想要什麼類型的節流?在問題文本中指定,而不是註釋。 –

+0

你試過了什麼? – tym32167

回答

0

如果您在.NET 4.0中有await,那麼您也有TaskEx.WhenAny

的選項有:

  1. Build an AsyncSemaphore
  2. 使用TPL Dataflow(從here
  3. 使用Reactive Extensions(從here

對於任何這些方法中,IEnumerable<T>返回類型可能是不正確的,除非你想要在第一個結果可用之前下載所有內容。