で複数の記事を実行し、私の構文ですが、私は私のライン上のコンパイルエラーがParallel.ForEach()
はここParallel.ForEach
のように使用されてい保ちますSystem.Data.DataRowがタイプであるが、可変
私は確かに私はただ見落としている単純なものです。以下は私の完全な構文です、誰かが私が逃しているものを私に助けることができれば、私はそれを高く評価します!未割り当ての
使用: -
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; } } }
EDIT
は@Glenトーマスで下記に概説の例に続いて、私はのコンパイルエラーを提示しif (grds.Tables[0].Rows.Count == 1) { Parallel.ForEach(rows, row => { dic.Add("userID", reader.GetValue(0).ToString()); //More Here } }
に私のコードを変更しましたローカル変数 'リーダー'
しかし、私はreader
を私の方法の先頭に宣言しましたか?
@コーディングゴリラParallel.ForEachの下にあるコードの大部分は、彼が実際に何を達成するために必要なのでしょうか? –
RE:未定義のリーダー - 宣言しましたが、インスタンス化されていません。コンパイラは何も割り当てないと判断し、コンパイルを拒否します。これは実行時に 'NullReferenceException'を受け取る安全機構です。 – CodingGorilla
もう一度、答えは例外です - あなたが何かを割り当てない限り、コンパイラは読者の使用を受け入れません。 –