ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Task应用

2022-08-18 09:33:01  阅读:143  来源: 互联网

标签:Task Console 应用 Replace tokenSource key new tranCount


 1 tokenSource = new CancellationTokenSource();
 2 totalprocesstask = Task.Run( () => {
 3                         if (isfirst)
 4                         {
 5                              TotalTestThreadFunc(isfirst, tokenSource);
 6                         }
 7                         else
 8                         {
 9                             while (!tokenSource.IsCancellationRequested)//判断任务是否被取消
10                             {
11                                  TotalTestThreadFunc(isfirst, tokenSource);
12                             }
13                         }
14 
15                     }, tokenSource.Token);
16 
17                 //bool res = totalprocesstask.Result;//返回任务结果
18                 //单任务结束
19                 totalprocesstask.ContinueWith((last) =>
20                 {
21                     tokenSource.Cancel();//任务取消
22                     BindResultData(PredicateBuilder.True<SunnyResult>());
23                     ProcessColorEvent(0, "Stop");
24                 });//等待任务完成之后任务回调
25 
26                 //多任务结束
27                 //TaskFactory taskFactory = new TaskFactory();
28                 //taskFactory.ContinueWhenAll(tasks.ToArray(), t => { Console.WriteLine($"项目经理发布,测试人员测试任务 id:{Thread.CurrentThread.ManagedThreadId} time:{DateTime.Now}"); });
29 
30                 //异常处理
31                 try
32                 {
33                     totalprocesstask.Wait();//等待完成
34                 }
35                 catch (Exception ex)
36                 {
37                     Console.WriteLine(ex.Message);
38                 }
39                 finally
40                 {
41                     tokenSource.Dispose();
42                 }
43 
44 
45 
46 sfirst = false;
47 bool result = true;
48 
49 try
50 {
51  //开启事务
52 _unitOfWork.BeginTran();
53 
54 /////////////////////////////////////////for循环判断是否取消
55 if (cancellation.Token.IsCancellationRequested)
56                         {
57                             Console.WriteLine($"任务已取消 index:{i} id:{Thread.CurrentThread.ManagedThreadId} ");
58                             cancellation.Token.ThrowIfCancellationRequested();
59                         }
60  _sunnyResultServices.UpdateInSplit(DateTime.Now, it => new SunnyResult()
61                     {
62                         timeSpan = watch.ElapsedMilliseconds,
63                         deviceType = Model.Enums.GlobalEnumVars.ControlDeviceTypes.Camera,
64                         createTime = DateTime.Now,
65                         resultParams = dic.Count == 0 ? null : JsonConvert.SerializeObject(dic.Select(key => new { x = key.Key, y = key.Value["result"] }).ToDictionary(x => x.x, x => x.y)).Replace("\"", "").Replace(",", " , ").Replace("{", "").Replace("}", "").Replace('"', ' ').Trim(),
66                         NGParams = dic.Count == 0 ? null : JsonConvert.SerializeObject(dic.Select(key => new { x = key.Key, y = key.Value["NG"] }).ToDictionary(x => x.x, x => x.y)).Replace("\"", "").Replace(",", " , ").Replace("{", "").Replace("}", "").Replace('"', ' ').Trim(),
67                         result = result
68                     }, p => p.guid == resultid);
69 
70  _unitOfWork.CommitTran();
71 }
72 catch (Exception error)
73             {
74                 _unitOfWork.RollbackTran();
75                 LogHelper.Log(NLog.LogLevel.Error, this.GetType().FullName, System.Reflection.MethodBase.GetCurrentMethod().Name, error.ToString());
76 
77             }
View Code
 1 private int _tranCount { get; set; }
 2 public UnitOfWork()
 3         {
 4             _sqlSugarClient = DbScoped.SugarScope;
 5             _tranCount = 0;
 6         }
 7 public void BeginTran()
 8         {
 9             lock (this)
10             {
11                 _tranCount++;
12                 GetDbClient().BeginTran();
13             }
14         }
15 
16         public void CommitTran()
17         {
18             lock (this)
19             {
20                 _tranCount--;
21                 if (_tranCount == 0)
22                 {
23                     try
24                     {
25                         GetDbClient().CommitTran();
26                     }
27                     catch (Exception ex)
28                     {
29                         Console.WriteLine(ex.Message);
30                         GetDbClient().RollbackTran();
31                     }
32                 }
33             }
34         }
35 
36         public void RollbackTran()
37         {
38             lock (this)
39             {
40                 _tranCount--;
41                 GetDbClient().RollbackTran();
42             }
43         }
View Code
BaseRepository: protected ISqlSugarClient DbBaseClient => _dbBase;   

 

标签:Task,Console,应用,Replace,tokenSource,key,new,tranCount
来源: https://www.cnblogs.com/1228941830ying/p/16580651.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有