C# - Task.WaitAll () 不等待所有任务完成
C# - Task.WaitAll () is not waiting for all task to finish
我正在使用 C# 代码将 Xml 发送到 api 端点并捕获响应
我的做法如下
我的文件夹 A 有 100 xmls,文件夹 B 有 100 Xmls,文件夹 C 有 100 xmls
我循环遍历每个文件夹,并在每次循环迭代中创建一个任务。让我们将其称为文件夹任务
文件夹任务循环遍历每个文件夹中的所有 xml 并捕获响应。这是在 sendrequestandcaptureresponse() 方法中完成的
我面临的问题是在处理所有 xml 之前循环结束。所有 300 XMLS(位于文件夹 A、B 和 C 中)都触发了 sendrequestandcaptureresponse() 方法,但我只收到 150(大约)XMLS
的响应
Task.WaitAll(任务列表)在等待所有 XML 响应之前退出
请在下面找到代码
遍历文件夹的代码
foreach(Folder in Folders){
Task t=Task.Factory.StartNew(
async () =>
{
Httpmode.RunRegressionInHTTPMode(folderPath);
}
}).Unwrap();
tasklist.Add(t);
}
Task.WaitAll(tasklist.ToArray());
发送请求和捕获响应的代码
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 方法,但仍然无法等到所有任务完成。
任何帮助都会很棒。
提前致谢
添加下面的 SendRequestAndCaptureResponse 代码
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;
}
}
您没有等待内部任务:
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);
(不区分错别字)
我正在使用 C# 代码将 Xml 发送到 api 端点并捕获响应 我的做法如下 我的文件夹 A 有 100 xmls,文件夹 B 有 100 Xmls,文件夹 C 有 100 xmls
我循环遍历每个文件夹,并在每次循环迭代中创建一个任务。让我们将其称为文件夹任务
文件夹任务循环遍历每个文件夹中的所有 xml 并捕获响应。这是在 sendrequestandcaptureresponse() 方法中完成的
我面临的问题是在处理所有 xml 之前循环结束。所有 300 XMLS(位于文件夹 A、B 和 C 中)都触发了 sendrequestandcaptureresponse() 方法,但我只收到 150(大约)XMLS
的响应Task.WaitAll(任务列表)在等待所有 XML 响应之前退出
请在下面找到代码
遍历文件夹的代码
foreach(Folder in Folders){
Task t=Task.Factory.StartNew(
async () =>
{
Httpmode.RunRegressionInHTTPMode(folderPath);
}
}).Unwrap();
tasklist.Add(t);
}
Task.WaitAll(tasklist.ToArray());
发送请求和捕获响应的代码
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 方法,但仍然无法等到所有任务完成。
任何帮助都会很棒。
提前致谢
添加下面的 SendRequestAndCaptureResponse 代码
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;
}
}
您没有等待内部任务:
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);
(不区分错别字)