我想在我的列表中进行异步循环,并在paraller中执行方法Execute。我很想知道如何实现这一目标。
这是我的清单:
public class MakeRequest
{
public string file_READYTOTEST { get; set; }
public string file_DONEFILE { get; set; }
public string Inventory_name { get; set; }
public string TimeOutFP { get; set; }
public Server server { get; set; }
}
在最后我有很多元素被其他foreach添加到这个列表
IList<MakeRequest> makeRequestList;
makeRequestList.Add(new MakeRequest { file_DONEFILE = DONEFILE, file_READYTOTEST = READYTOTEST, Inventory_name = file, server = SAMaddress, TimeOutFP = IO.TimeOutFP } );
并且拥有该对象列表我需要进行一些计算
说明:
file_DONEFILE = final file name to be saved (string)
file_READYTOTEST = file which will be opened to do some operation then result save to file_DONEFILE (string)
Inventory_name = my inventory item (string)
server = server ip (string)
Timeout = path of file (string)
当我在同步中执行时,我正在执行这样的操作:
foreach (MakeRequest item in makerequest)
{
Execute(item.file_READYTOTEST, item.file_DONEFILE, item.Inventory_name, IO.TimeOutFP, item.server);
}
这是我的方法:
//Create a soap webrequest to [Url]
public static HttpWebRequest CreateWebRequest(string ServerIP)
{
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(ServerIP);
webRequest.Headers.Add(@"SOAP:Action");
webRequest.ContentType = "text/xml;charset=\"utf-8\"";
webRequest.Accept = "text/xml";
webRequest.Method = "POST";
return webRequest;
}
public static void Execute(string xmlFileRequest, string xmlFileResult, string InventoryElement, string TimeOutFile, Server server)
{
HttpWebRequest request = CreateWebRequest(server.ServerIP);
var soapEnvelopeXml = new XmlDocument();
soapEnvelopeXml.Load(xmlFileRequest);
using (Stream stream = request.GetRequestStream())
{
soapEnvelopeXml.Save(stream);
}
try
{
using (WebResponse response = request.GetResponse())
{
using (var rd = new StreamReader(response.GetResponseStream()))
{
string soapResult = rd.ReadToEnd();
var xdoc = new XmlDocument();
xdoc.LoadXml(soapResult);
xdoc.Save(xmlFileResult);
}
}
}
catch (WebException e)
{
using (StreamWriter sw = File.AppendText(TimeOutFile))
{
sw.WriteLine(InventoryElement);
}
}
}
你能帮我解决一下我如何让它异步。
更新NR 2关于进一步讨论的主题:
foreach (MakeRequest item in makeRequestList)
{
//1) no definition for run ...
Task.Run(() => Execute(item.file_READYTOTEST, item.file_DONEFILE, item.Inventory_name, IO.TimeOutFP, item.server));
}
//2. I want to wait on all threads to end before Console.Writeline("All...
Task.WaitAll(task);
Console.Writeline("All Tasks have been done")
Console.Readline()
//other piece of code:
//3. Catch case: (where to define locker?)
}
catch (WebException e)
{
lock(locker)
{
using (StreamWriter sw = File.AppendText(TimeOutFile))
{
sw.WriteLine(InventoryElement);
}
}
}
更新NR 3关于进一步讨论的主题:
List<Task> mytasks = new List<Task>();
foreach (MakeRequest item in makeRequestList)
{
Task myFirstTask = Task.Factory.StartNew(() => Execute(item.file_READYTOTEST, item.file_DONEFILE, item.Inventory_name, IO.TimeOutFP, item.server) );
mytasks.Add(myFirstTask);
}
Task.WaitAll(mytasks.ToArray());
Console.writeline("Something");
Console.readline();
答案 0 :(得分:0)
您可以使用Task
将其设为异步:
foreach (MakeRequest item in makerequest)
{
Task.Run( () => Execute(item.file_READYTOTEST, item.file_DONEFILE, item.Inventory_name, IO.TimeOutFP, item.server) );
}
答案 1 :(得分:0)
如果你的工作框架大于或等于3.5,你可以使用线程类运行到另一个循环。
Thread threadObj = null;
foreach (MakeRequest item in makerequest)
{
threadObj = new Thread(new ThreadStart(()=>Execute(item.file_READYTOTEST, item.file_DONEFILE, item.Inventory_name, IO.TimeOutFP, item.server) ));
}