我是急于找到一个解决我的问题。
以下是其中的为每个项目不同的任务列表与LT的代码; AccountContactView方式>
列表< AccountContactViewModel> selectedDataList = DataList.Where(的DataList =>(布尔)dataList.GetType() .GetProperty(器isChecked) .GetValue(DataList控件,新的对象[0 ])==真).ToList(); this.IsEnabled = FALSE; 任务validateMarked = Task.Factory.StartNew(()=> {的foreach(AccountContactViewModel视图模型在selectedDataList) {如果(视图模型!= NULL) {任务的validate = Task.Factory.StartNew(()=> ValidateAccount(视图模型),(TaskCreationOptions)TaskContinuationOptions.AttachedToParent); } } }); validateMarked.ContinueWith(X => this.IsEnabled = TRUE);现在我的问题是,它运行时,它只能运行在数组中的最后一项。什么我做错了任何想法?
我不希望使用 Parallel.ForEach 监守它doesn 'T提供的并行,以增加基于每一个任务的完成进度条的必然结果。
解决方案这可能是一个lambda范围问题。
您是否尝试过视图模型将它传递给StartNew方法
... 任务validateMarked = Task.Factory.StartNew(()=> {的foreach(AccountContactViewModel视图模型在selectedDataList) { VAR localViewModel =视图模型; 如果(localViewModel!= NULL) {任务的validate = Task.Factory.StartNew(()=> ValidateAccount (localViewModel),(TaskCreationOptions)TaskContinuationOptions.AttachedToParent); } } }); ...
I am in desperate to find a solution to my problem.
Following is the code which generates different task for each item in List<AccountContactView>.
List<AccountContactViewModel> selectedDataList = DataList.Where( dataList => (bool) dataList.GetType() .GetProperty("IsChecked") .GetValue(dataList, new object[0]) == true ).ToList(); this.IsEnabled = false; Task validateMarked = Task.Factory.StartNew(() => { foreach (AccountContactViewModel viewModel in selectedDataList) { if (viewModel != null) { Task validate = Task.Factory.StartNew( () => ValidateAccount(viewModel), (TaskCreationOptions)TaskContinuationOptions.AttachedToParent); } } }); validateMarked.ContinueWith(x => this.IsEnabled = true);Now my problem is when it runs, it only runs for the last item in the array. Any idea about what I am doing wrong?
I don't want to use Parallel.ForEach becuase it doesn't provide the necessary effect of parallelism to increase the progress bar based on completion of each task.
解决方案This might be a lambda scope problem.
Have you tried to assign the viewModel to a local variable before passing it to the StartNew method
... Task validateMarked = Task.Factory.StartNew(() => { foreach (AccountContactViewModel viewModel in selectedDataList) { var localViewModel = viewModel; if (localViewModel != null) { Task validate = Task.Factory.StartNew( () => ValidateAccount(localViewModel), (TaskCreationOptions)TaskContinuationOptions.AttachedToParent); } } }); ...
更多推荐
只有最后一个任务运行!
发布评论