使用 Parallel.ForEach 执行多个帖子
Perform Multiple Posts WIth Parallel.ForEach
这是我的语法,但我的行上一直有一个编译错误 Parallel.ForEach()
System.Data.DataRow is a type but is used like a variable
我确信这很简单,我只是忽略了。以下是我的完整语法,如果有人能帮助我解决我所缺少的,我将不胜感激!
private void TryParallel()
{
Dictionary<string, string> dic = new Dictionary<string, string>();
string strEndpointURL = string.Format("http://sitetosenddatato.com/post");
SqlDataReader reader;
string strPostData = "";
string strMessage = "";
DataSet grds = new DataSet();
grds = GetSQLResults();
if (grds.Tables[0].Rows.Count >= 1)
{
Parallel.ForEach(DataRow, grds.Tables[0].Rows =>
{
dic.Add("userID", reader.GetValue(0).ToString());
dic.Add("name", reader.GetValue(1).ToString());
dic.Add("address", reader.GetValue(2).ToString());
dic.Add("city", reader.GetValue(3).ToString());
dic.Add("state", reader.GetValue(4).ToString());
dic.Add("zip", reader.GetValue(5).ToString());
dic.Add("Phone", reader.GetValue(6).ToString());
});
}
System.Web.Script.Serialization.JavaScriptSerializer json = new System.Web.Script.Serialization.JavaScriptSerializer();
foreach (var d in dic) { strPostData += d.Key + "=" + Server.UrlEncode(d.Value) + "&"; }
strPostData += "hs_context=";
S ystem.Net.HttpWebRequest r = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(strEndpointURL);
r.Method = "POST";
r.Accept = "application/json";
r.ContentType = "application/x-www-form-urlencoded";
r.ContentLength = strPostData.Length;
r.KeepAlive = false;
using (System.IO.StreamWriter sw = new System.IO.StreamWriter(r.GetRequestStream()))
{
try { sw.Write(strPostData); }
catch (Exception ex) { strMessage = ex.Message; }
}
var response = r.GetResponse();
Stream receiveStream = response.GetResponseStream();
StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8);
var result = readStream.ReadToEnd();
var xml = System.Xml.Linq.XElement.Parse(result);
if (xml.Elements("success").FirstOrDefault().Value == "1") { strMessage = "Success"; }
else
{
var errors = xml.Elements("errors");
foreach (var error in errors.Elements("error")) { strMessage = error.Value; }
}
}
编辑
按照@Glen Thomas 下面概述的示例 - 我将代码更改为
if (grds.Tables[0].Rows.Count == 1)
{
Parallel.ForEach(rows, row =>
{
dic.Add("userID", reader.GetValue(0).ToString());
//More Here
}
}
出现编译错误:
Use of unassigned local variable 'reader'
但是我在方法的顶部声明了 reader
?
您正在指定一个类型名称作为第一个参数。这应该是您正在迭代的集合。第二个参数是要执行的函数,集合中的每个元素都有一个参数。
Parallel.ForEach的正确用法是这样的:
var rows = new DataRow[0]
Parallel.ForEach(rows, row =>
{
// Do something with row here
});
对于您的代码:
Parallel.ForEach(grds.Tables[0].Rows.OfType<DataRow>(), row =>
{
dic.Add("userID", reader.GetValue(0).ToString());
dic.Add("name", reader.GetValue(1).ToString());
dic.Add("address", reader.GetValue(2).ToString());
dic.Add("city", reader.GetValue(3).ToString());
dic.Add("state", reader.GetValue(4).ToString());
dic.Add("zip", reader.GetValue(5).ToString());
dic.Add("Phone", reader.GetValue(6).ToString());
});
我相信你想这样做:
Parallel.ForEach(grds.Tables[0].Rows.OfType<DataRow>(), (row) =>
{
dic.Add("userID", reader.GetValue(0).ToString());
dic.Add("name", reader.GetValue(1).ToString());
dic.Add("address", reader.GetValue(2).ToString());
dic.Add("city", reader.GetValue(3).ToString());
dic.Add("state", reader.GetValue(4).ToString());
dic.Add("zip", reader.GetValue(5).ToString());
dic.Add("Phone", reader.GetValue(6).ToString());
//though realistically you should be doing something with your specific row
});
答案在您收到的错误消息中 - DataRow 未在您提供的代码中定义为对象。
然而,这甚至没有真正解决您的实际问题,我认为这是并行执行多个 HTTP posts - 所以您需要将您的 post 逻辑放在匿名中你的 Parallel.ForEach()
的功能
这是我的语法,但我的行上一直有一个编译错误 Parallel.ForEach()
System.Data.DataRow is a type but is used like a variable
我确信这很简单,我只是忽略了。以下是我的完整语法,如果有人能帮助我解决我所缺少的,我将不胜感激!
private void TryParallel()
{
Dictionary<string, string> dic = new Dictionary<string, string>();
string strEndpointURL = string.Format("http://sitetosenddatato.com/post");
SqlDataReader reader;
string strPostData = "";
string strMessage = "";
DataSet grds = new DataSet();
grds = GetSQLResults();
if (grds.Tables[0].Rows.Count >= 1)
{
Parallel.ForEach(DataRow, grds.Tables[0].Rows =>
{
dic.Add("userID", reader.GetValue(0).ToString());
dic.Add("name", reader.GetValue(1).ToString());
dic.Add("address", reader.GetValue(2).ToString());
dic.Add("city", reader.GetValue(3).ToString());
dic.Add("state", reader.GetValue(4).ToString());
dic.Add("zip", reader.GetValue(5).ToString());
dic.Add("Phone", reader.GetValue(6).ToString());
});
}
System.Web.Script.Serialization.JavaScriptSerializer json = new System.Web.Script.Serialization.JavaScriptSerializer();
foreach (var d in dic) { strPostData += d.Key + "=" + Server.UrlEncode(d.Value) + "&"; }
strPostData += "hs_context=";
S ystem.Net.HttpWebRequest r = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(strEndpointURL);
r.Method = "POST";
r.Accept = "application/json";
r.ContentType = "application/x-www-form-urlencoded";
r.ContentLength = strPostData.Length;
r.KeepAlive = false;
using (System.IO.StreamWriter sw = new System.IO.StreamWriter(r.GetRequestStream()))
{
try { sw.Write(strPostData); }
catch (Exception ex) { strMessage = ex.Message; }
}
var response = r.GetResponse();
Stream receiveStream = response.GetResponseStream();
StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8);
var result = readStream.ReadToEnd();
var xml = System.Xml.Linq.XElement.Parse(result);
if (xml.Elements("success").FirstOrDefault().Value == "1") { strMessage = "Success"; }
else
{
var errors = xml.Elements("errors");
foreach (var error in errors.Elements("error")) { strMessage = error.Value; }
}
}
编辑
按照@Glen Thomas 下面概述的示例 - 我将代码更改为
if (grds.Tables[0].Rows.Count == 1)
{
Parallel.ForEach(rows, row =>
{
dic.Add("userID", reader.GetValue(0).ToString());
//More Here
}
}
出现编译错误:
Use of unassigned local variable 'reader'
但是我在方法的顶部声明了 reader
?
您正在指定一个类型名称作为第一个参数。这应该是您正在迭代的集合。第二个参数是要执行的函数,集合中的每个元素都有一个参数。
Parallel.ForEach的正确用法是这样的:
var rows = new DataRow[0]
Parallel.ForEach(rows, row =>
{
// Do something with row here
});
对于您的代码:
Parallel.ForEach(grds.Tables[0].Rows.OfType<DataRow>(), row =>
{
dic.Add("userID", reader.GetValue(0).ToString());
dic.Add("name", reader.GetValue(1).ToString());
dic.Add("address", reader.GetValue(2).ToString());
dic.Add("city", reader.GetValue(3).ToString());
dic.Add("state", reader.GetValue(4).ToString());
dic.Add("zip", reader.GetValue(5).ToString());
dic.Add("Phone", reader.GetValue(6).ToString());
});
我相信你想这样做:
Parallel.ForEach(grds.Tables[0].Rows.OfType<DataRow>(), (row) =>
{
dic.Add("userID", reader.GetValue(0).ToString());
dic.Add("name", reader.GetValue(1).ToString());
dic.Add("address", reader.GetValue(2).ToString());
dic.Add("city", reader.GetValue(3).ToString());
dic.Add("state", reader.GetValue(4).ToString());
dic.Add("zip", reader.GetValue(5).ToString());
dic.Add("Phone", reader.GetValue(6).ToString());
//though realistically you should be doing something with your specific row
});
答案在您收到的错误消息中 - DataRow 未在您提供的代码中定义为对象。
然而,这甚至没有真正解决您的实际问题,我认为这是并行执行多个 HTTP posts - 所以您需要将您的 post 逻辑放在匿名中你的 Parallel.ForEach()
的功能