首页新闻找找看学习计划

Task异步方法调用出现重复值

0
悬赏园豆:100 [已解决问题] 解决于 2019-08-30 15:45
复制代码
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行值是重复的,这怎么改呢?

happydaily的主页 happydaily | 小虾三级 | 园豆:579
提问于:2019-08-29 17:18
< >
分享
最佳答案
0

本地变量引起的问题。
你的代码,没复制完整吧?在foreach过程中,json是不断的改变吧?
参考第一个示例:https://docs.microsoft.com/zh-cn/dotnet/api/system.threading.tasks.taskfactory.startnew

收获园豆:50
pencile | 菜鸟二级 |园豆:448 | 2019-08-29 22:30

代码没有完整,核心就这个,上面的循环调用下面的,Json是每条都不一样的。

happydaily | 园豆:579 (小虾三级) | 2019-08-30 08:02

@happydaily: 所以task还没启动,循环中的json已经变好好几回了,等task开始执行了,就会存在几个task拿到一样的json。照msdn上的示例一修改你的代码就可以了

pencile | 园豆:448 (菜鸟二级) | 2019-08-30 09:04

@pencile: 原来是这么回事,谢谢。

happydaily | 园豆:579 (小虾三级) | 2019-08-30 10:23
其他回答(1)
0

应该这么写

var tasks = new List<Task>();
 foreach (var po in _Positions)
            {
                string json = "{}";
                Thread.Sleep(30);
                //HttpHelperSync.UpdatePositionSync(json, servername);//同步
                //Task.Run(() => HttpHelperSync.UpdatePositionSync(json, servername));//异步
               tasks.Add(Task.Run(() => HttpHelperSync.UpdatePositionSync(json, servername)));
            }
Task.WaitAll(task.ToArray());

收获园豆:50
Shendu.CC | 园豆:1947 (小虾三级) | 2019-08-29 21:11

不行不行,这个代码更坑-_-,原来最多一条重复两次,用了这个代码最多一条重复了几十次。

支持(0) 反对(0) happydaily | 园豆:579 (小虾三级) | 2019-08-30 08:01

@happydaily: 好吧,😂
那我建议不要定义json,而是把那个拼接的string直接当参数传入,我就不信还会重复。

支持(0) 反对(0) Shendu.CC | 园豆:1947 (小虾三级) | 2019-08-30 09:35

@Shendu.CC: 这个不拼接怎么办呢,循环的目的就是为了生成不同的JSON啊

支持(0) 反对(0) happydaily | 园豆:579 (小虾三级) | 2019-08-30 10:30

@happydaily:

Task.Run(()=>HttpHelperSync.UpdatePositionSync("{\"id\":\"" + po.Key_Id + "\",\"name\":\"" + po.Value + "\",\"remark\":\"" + po.Comments + "\"}",servername)));
支持(0) 反对(0) Shendu.CC | 园豆:1947 (小虾三级) | 2019-08-30 10:38

@happydaily:感觉这样改也不行,还是按照楼下的解决方案吧。StartNew

支持(0) 反对(0) Shendu.CC | 园豆:1947 (小虾三级) | 2019-08-30 10:40
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册