public static void TransDutySync(IEnumerable<KeyValueEntity> _Positions, int servername) { foreach (var po in _Positions) { string json = "{\"id\":\"" + po.Key_Id + "\",\"name\":\"" + po.Value + "\",\"remark\":\"" + po.Comments + "\"}";
Thread.Sleep(30); HttpHelperSync.UpdatePositionSync(json, servername);//同步 Task.Run(() => HttpHelperSync.UpdatePositionSync(json, servername));//异步 } } public static string HttpPostSync(UrlCollection urlCollection) { string json = urlCollection.json; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(urlCollection.Url); request.Method = "POST"; request.ContentType = "application/json"; request.Headers.Add(HttpRequestHeader.Authorization,"Basic "+ urlCollection.Token); string retString = string.Empty;int StatusCode = 0; try { Stream myRequestStream = request.GetRequestStream(); StreamWriter myStreamWriter = new StreamWriter(myRequestStream, Encoding.GetEncoding("utf-8")); myStreamWriter.Write(json); myStreamWriter.Close(); HttpWebResponse response = request.GetResponse() as HttpWebResponse; StatusCode = (int)response.StatusCode; Stream myResponseStream = response.GetResponseStream(); StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8")); retString = myStreamReader.ReadToEnd(); myStreamReader.Close(); myResponseStream.Close(); } catch (WebException ex) { retString = ex.Message; HttpWebResponse response = (HttpWebResponse)ex.Response; StatusCode = (int)response.StatusCode; } RECLOUD_DATASYNC_LOG _log = new RECLOUD_DATASYNC_LOG(); _log.LOGTIME = DateTime.Now; _log.LOGTITLE = logTitle; _log.RESPONSECODE = StatusCode; _log.LOGCONTENT = json; _log.RESPONSECONTENT = retString; _log.SERVERNAME = urlCollection.ServerName;//这个值每个请求是一样的 HttpHelper.WriteLog(_log);//写数据库日志 return retString; }
请教一下,如果写成同步方法,是没有问题的,如果写成异步形式,358行更新,数据库中就出现50-70行值是重复的,这怎么改呢?
相关问题
- How can I convince powershell (run through task sc
- Does Task.ConfigureAwait(false) on the last method
- UnobservedTaskException is not killing the process
- Run same code multiple times in parallel with diff
- Rake Default Task and Namespaces
相关文章
- 异步调用等待期间数据被篡改导致数据不一致问题!
- mysql我在insert的时候设置了一个开始时间new Date(),启动异步方法,修改这条数据,
- How do do an async ServiceController.WaitForStatus
- How to check if an activity is locked (app pinning
- await Task.CompletedTask for what?
- Why is the call ambiguous? 'Task.Run(Action)
- Save content of Email body in outlook to a file
- Task not serializable while using custom dataframe
本地变量引起的问题。
你的代码,没复制完整吧?在foreach过程中,json是不断的改变吧?
参考第一个示例:https://docs.microsoft.com/zh-cn/dotnet/api/system.threading.tasks.taskfactory.startnew
应该这么写