私はasp.net mvc-4 Webアプリケーションで作業しています。しかし、これら2つのアプローチを繰り返し使用することの違いは何ですかリスト上およびWebクライアント()を呼び出しを開始: -Parallel.ForEachとTask.Factory.StartNewを使用する主な違いは何ですか?
アプローチ-1
Parallel.ForEach(photos,new ParallelOptions { MaxDegreeOfParallelism = 7 }, p =>
{
ResourceAccountListInfo resourceAccountListInfo = new ResourceAccountListInfo();
WebClient wc = new WebClient();
var json = wc.DownloadString(p.url);
resourceAccountListInfo = JsonConvert.DeserializeObject<ResourceAccountListInfo>(json);
if (resourceAccountListInfo.operation.Details.CUSTOMFIELD.Count > 0)
{
List<CUSTOMFIELD> customfield = resourceAccountListInfo.operation.Details.CUSTOMFIELD.Where(a =>
a.CUSTOMFIELDLABEL.ToLower() == "name"
).ToList();
if (customfield.Count == 1)
{
PMresourcesOnly.Add(resourceAccountListInfo.operation.Details);
}
}
//code goes here
});
アプローチ-2
foreach (Photo p in photos)
{
Task.Factory.StartNew(() =>
{
ResourceAccountListInfo resourceAccountListInfo = new ResourceAccountListInfo();
WebClient wc = new WebClient();
var json = wc.DownloadString(p.url);
resourceAccountListInfo = JsonConvert.DeserializeObject<ResourceAccountListInfo>(json);
if (resourceAccountListInfo.operation.Details.CUSTOMFIELD.Count > 0)
{
List<CUSTOMFIELD> customfield = resourceAccountListInfo.operation.Details.CUSTOMFIELD.Where(a =>
a.CUSTOMFIELDLABEL.ToLower() == "name"
).ToList();
if (customfield.Count == 1)
{
PMresourcesOnly.Add(resourceAccountListInfo.operation.Details);
}
}
//code goes here
});
}
をおかげ
すべてのスレッドはすべて「resourceAccountListInfo」を置き換えますか?それは間違っていると思われる –
違いは:Parallel.ForEachすべての "写真"が終了するまで待ってから、続けてください。 Task.Factory.StartNewでは、写真が終了したかどうかに関わらず、常に続行されます。 –
@ScottChamberlain申し訳ありませんが、私の質問に関連するコードの一部だけを示しました..今、各反復の中にresourceAccountListInfoの余分な処理があります... –