Browse Source

添加暂停取消

user_lt 1 năm trước cách đây
mục cha
commit
3c92f9a2c9

+ 45 - 28
BlankApp1/BlankApp1/ViewModels/BusinessManageViewModel/AutoTestViewModel.cs

@@ -62,14 +62,19 @@ namespace PLCTool.ViewModels.BusinessManageViewModel
             CloseCommand = new DelegateCommand(Close);
             StartCommand = new DelegateCommand(Start);
             StopOrContinueCommand = new DelegateCommand<object>(StopOrContinueMethod);
-          
-            BeforeConList = new ObservableCollection<BasPlcItemConfigDto>();
+            CancelCommand = new DelegateCommand<object>(Cancel);
+              BeforeConList = new ObservableCollection<BasPlcItemConfigDto>();
             InConList = new ObservableCollection<BasPlcItemConfigDto>();
             OutConList = new ObservableCollection<BasPlcItemConfigDto>();
             GetConfigOption();
 
         }
 
+        private void Cancel(object obj)
+        {
+            tokensource.Cancel();
+        }
+
         private void StopOrContinueMethod(object obj)
         {
             if(StopOrContinueCont=="暂停")
@@ -424,36 +429,48 @@ namespace PLCTool.ViewModels.BusinessManageViewModel
         /// 开始检测
         /// </summary>
         /// <param name="obj"></param>
-        private async void Start()
+        private  void Start()
         {
- 
-            ///查找方案下的所有方案明细,并排序,
-            var basicSchDtls = _basicPlcTestSchemeDtlService.FindAllBySchId(schId)?.OrderBy(x => x.scheme_dtl_id);
-            foreach (var item in basicSchDtls)
+            CancellationToken cancellationToken = tokensource.Token;
+            Task.Run(async() =>  //模拟耗时任务
             {
-                long schDetailId = item.scheme_dtl_id;
-                //编号小于当前选择的就不测试了,
-                if(schDetailId < selctStartDetailId)
+
+                var basicSchDtls = _basicPlcTestSchemeDtlService.FindAllBySchId(schId)?.OrderBy(x => x.scheme_dtl_id);
+                foreach (var item in basicSchDtls)
                 {
-                    continue;
-                }
+                    if (cancellationToken.IsCancellationRequested)
+                    {
+                        return;
+                    }
+                    m.WaitOne(); //当m等于true,才会往下执行,否则一直在此等待
+                    long schDetailId = item.scheme_dtl_id;
+                    //编号小于当前选择的就不测试了,
+                    if (schDetailId < selctStartDetailId)
+                    {
+                        continue;
+                    }
 
-                globalSchDetailId = schDetailId;
-                await ExecTest();
-                //更新下一个界面显示
+                    globalSchDetailId = schDetailId;
+                    await ExecTest();
+                    //更新下一个界面显示
                     int id = Convert.ToInt32(globalSchDetailId);
                     //界面显示
                     System.Windows.Application.Current.Dispatcher.Invoke((delegate
                     {
                         GetDetailInfo(id);
                     }));
-                //最后一项必须要测试
-                if (globalSchDetailId == basicSchDtls.Max(x => x.scheme_dtl_id))
-                {
-                    await ExecTest();
-                }
+                    //最后一项必须要测试
+                    if (globalSchDetailId == basicSchDtls.Max(x => x.scheme_dtl_id))
+                    {
+                        await ExecTest();
+                        MessageBox.Show("已是此测试方案最后一个测试项!", "确认", MessageBoxButton.OK, MessageBoxImage.Information);
 
-            }
+                    }
+
+                }
+            }, cancellationToken);//绑定令牌到多线程
+            ///查找方案下的所有方案明细,并排序,
+          
 
         }
         private async Task ExecTest()
@@ -834,11 +851,11 @@ namespace PLCTool.ViewModels.BusinessManageViewModel
             AddOrEditTesDtltRecord(globalSchDetailId);
 
             //判定是否为最后一项
-            long maxDtlId = _basicPlcTestSchemeDtlService.FindAllBySchId(schId).Select(x => x.scheme_dtl_id).Max();
-            if (globalSchDetailId == maxDtlId)
-            {
-                MessageBox.Show("已是此测试方案最后一个测试项!", "确认", MessageBoxButton.OK, MessageBoxImage.Information);
-            }
+            //long maxDtlId = _basicPlcTestSchemeDtlService.FindAllBySchId(schId).Select(x => x.scheme_dtl_id).Max();
+            //if (globalSchDetailId == maxDtlId)
+            //{
+            //    MessageBox.Show("已是此测试方案最后一个测试项!", "确认", MessageBoxButton.OK, MessageBoxImage.Information);
+            //}
             return isJudge;
         }
         /// <summary>
@@ -1029,8 +1046,8 @@ namespace PLCTool.ViewModels.BusinessManageViewModel
 
         public DelegateCommand StartCommand { set; get; }
         public DelegateCommand<object> StopOrContinueCommand { set; get; }
-
-     
+        public DelegateCommand<object> CancelCommand { set; get; }
+        
 
 
         #endregion

+ 1 - 1
BlankApp1/BlankApp1/Views/BusinessManageView/AutoTestView.xaml

@@ -267,7 +267,7 @@
             <StackPanel Orientation="Horizontal"  HorizontalAlignment="Center">
                 <Button  Content="开始检测" Width="80"    Command="{Binding StartCommand}" Style="{StaticResource NormalButtonStyle}" />
                 <Button  Content="{Binding StopOrContinueCont}" Width="80"    Command="{Binding StopOrContinueCommand}" Style="{StaticResource NormalButtonStyle}" Margin="10,0,0,0" />
-                <Button  Content="停止" Width="80"    Command="{Binding StopCommand}" Style="{StaticResource NormalButtonStyle}" Margin="10,0,0,0" />
+                <Button  Content="停止" Width="80"    Command="{Binding CancelCommand}" Style="{StaticResource NormalButtonStyle}" Margin="10,0,0,0" />
             </StackPanel>
           
         </Grid>