|
@@ -19,4 +19,46 @@
|
19
|
19
|
> 用法
|
20
|
20
|
- async 用在方法定义前面,await只能写在带有async标记的方法中。
|
21
|
21
|
- 注意await异步等待的地方,await后面的代码和前面的代码执行的线程可能不一样
|
22
|
|
-- async关键字创建了一个状态机,类似yield return 语句;await会解除当前线程的阻塞,完成其他任务
|
|
22
|
+- async关键字创建了一个状态机,类似yield return 语句;await会解除当前线程的阻塞,完成其他任务
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+- 同步
|
|
26
|
+```
|
|
27
|
+比如写ProgressBarItem时不用async异步和Task接口,返回时,直接return model
|
|
28
|
+private ProgressBarViewModel GetProgressBarItem(string connKey, string projectId)
|
|
29
|
+ {
|
|
30
|
+ var model = new ProgressBarViewModel();
|
|
31
|
+ _projectDbContext = DDDbContextFactory.Create(connKey);
|
|
32
|
+ //先过滤掉不想要的条件
|
|
33
|
+ var exectedScenes = _projectDbContext.SceneResults.Where(t => t.Ratio != "0" && (DateTime.Now - DateTime.Parse(t.StartTime)).TotalSeconds > 0);
|
|
34
|
+
|
|
35
|
+ //根据不同情况判断
|
|
36
|
+ string progressBarDesc = string.Empty;
|
|
37
|
+ if(exectedScenes.Any(t=> decimal.Parse(t.Ratio) < 1.0m))
|
|
38
|
+ {
|
|
39
|
+ progressBarDesc = exectedScenes.Min(t => decimal.Parse(t.Ratio)).ToString();
|
|
40
|
+ }
|
|
41
|
+ else
|
|
42
|
+ {
|
|
43
|
+ progressBarDesc = "1";
|
|
44
|
+ }
|
|
45
|
+
|
|
46
|
+ model.ProjectId = projectId;
|
|
47
|
+ model.ConnKey = connKey;
|
|
48
|
+ model.ProgressBar = progressBarDesc;
|
|
49
|
+ return model;
|
|
50
|
+ }
|
|
51
|
+```
|
|
52
|
+- 异步
|
|
53
|
+```
|
|
54
|
+private async Task<StatusViewModel> GetIssuccessItem(string connKey, string projectId)
|
|
55
|
+ {
|
|
56
|
+ var model = new StatusViewModel();
|
|
57
|
+ _projectDbContext = DDDbContextFactory.Create(connKey);
|
|
58
|
+ model.ProjectId = projectId;
|
|
59
|
+ model.ConnKey = connKey;
|
|
60
|
+ model.IsSuccess = (await _projectDbContext.SceneResults.Where(t => t.IsSuccess == "0").CountAsync()) > 0 ? "0" : "1";
|
|
61
|
+ return model;
|
|
62
|
+ }
|
|
63
|
+异步时用await形式对应assync
|
|
64
|
+```
|