C# - Task.WaitAll()不等待所有任务完成
问题描述:
我正在使用C#代码将xml发送到api终点并捕获响应 我这样做的方式如下 我有文件夹A 100 xmls,文件夹B 100 xmls和文件夹C 100 xmlsC# - Task.WaitAll()不等待所有任务完成
我循环通过每个文件夹,并在每个循环迭代我创建一个任务。可以称之为文件夹任务
文件夹任务循环遍历每个文件夹中的所有xml并捕获响应。这是在sendrequestandcaptureresponse()方法中完成的
我面临的问题是在所有xml被处理之前的循环结束。对于所有300个XMLS(位于文件夹A,B和C中)触发sendrequestandcaptureresponse()方法,但我得到的响应仅为150(大约)XMLS
Task.WaitAll(tasklist)在等待所有XML响应
请在下面找到
代码的代码通过文件夹来遍历它发送请求并捕捉[R
foreach(Folder in Folders){
Task t=Task.Factory.StartNew(
async() =>
{
Httpmode.RunRegressionInHTTPMode(folderPath);
}
}).Unwrap();
tasklist.Add(t);
}
Task.WaitAll(tasklist.ToArray());
代码esponse
public void RunRegressionInHTTPMode(folderPath){
try
{
string directoryPath = CurrServiceSetting.GetDirectoryPath();
var filePaths = CreateBlockingCollection(folderPath+"\\Input\\");
int taskCount = Int32.Parse(CurrServiceSetting.GetThreadCount());
var tasklist = new List<Task>();
for (int i = 0; i < taskCount; i++)
{
var t=Task.Factory.StartNew(
async () =>
{
string fileName;
while (!filePaths.IsCompleted)
{
if (!filePaths.TryTake(out fileName))
{
continue;
}
try{
SendRequestAndCaptureResponse(fileName,CurrServiceSetting,CurrSQASettings,testreportlocationpath);
}
catch(Exception r)
{
Console.WriteLine("#####SOME Exception in SendRequestAndCaptureResponse "+r.Message.ToString());
}
}
}).Unwrap();
tasklist.Add(t);
}
Task.WaitAll(tasklist.ToArray());
}
catch(Exception e)
{
}
}
任何人都可以请指点我在这里失踪。我将任务作为异步任务,并在任务结束时添加了Unwrap方法,但仍无法等到所有任务完成。
任何帮助将是伟大的。
在此先感谢
下面的添加
public void SendRequestAndCaptureResponse(string fileName,ServiceSettings curServiceSettings,SQASettings CurrSQASettings,string testreportlocationpath){
XmlDocument inputxmldoc = new XmlDocument ();
Stream requestStream=null;
byte[] bytes;
DateTime requestsenttime=DateTime.Now;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create (url);
string responseStr = "";
bytes = Encoding.ASCII.GetBytes (str4);
try{
request.ContentType = "text/xml; encoding='utf-8'";
request.ContentLength = bytes.Length;
Credentials credentials = new Credentials();
request.Credentials = new NetworkCredential (CurrSQASettings.GetUserName(), CurrSQASettings.GetPassword());
request.Method = "POST";
request.Timeout = Int32.Parse(curServiceSettings.GetTimeOut());
//OVERRIDING TIME
requestsenttime=DateTime.Now;
requestStream = request.GetRequestStream ();
requestStream.Write (bytes, 0, bytes.Length);
requestStream.Close ();
}
catch(Exception e){
return;
}
HttpWebResponse response;
try
{
response = (HttpWebResponse)request.GetResponse ();
if (response.StatusCode == HttpStatusCode.OK)
{
allgood = true;
Stream responseStream = response.GetResponseStream ();
responseStr = new StreamReader (responseStream).ReadToEnd ();
XmlDocument xml = new XmlDocument ();
xml.LoadXml (responseStr);
xml.Save(resultantactualxmlpath);
response.Close();
responseStream.Close();
}
}
catch(Exception e){
return;
}
}
答
的SendRequestAndCaptureResponse代码你不等待内部任务:
foreach(Folder in Folders){
Task t=Task.Factory.StartNew(
async() =>
{
await Httpmode.RunRegressionInHTTPMode(folderPath); // <--- await here
}
}).Unwrap();
tasklist.Add(t);
}
Task.WaitAll(tasklist.ToArray());
为此,你的迭代不会等待内任务结束 - 在每次迭代中,您只需创建一个任务并继续。
更优雅的方式来创建任务将是:
var tasks = Folders.Select(p=> Httpmode.RunRegressionInHTTPMode(p)).ToArray();
Task.WaitAll(tasks);
(错字不敏感)
非常感谢Glorin。在这种情况下,请告知它期望的返回值。 ANY指针将会有所帮助。 –
不要等待'void'方法/委托:https://msdn.microsoft.com/en-us/magazine/jj991977.aspx –
如果抛出,会发生什么;而不是返回;在你添加的最新功能?在我看来,你正在隐藏潜在的错误。 –