只有最后一个任务运行!

编程入门 行业动态 更新时间:2024-10-07 20:34:03
本文介绍了只有最后一个任务运行!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我是急于找到一个解决我的问题。

以下是其中的为每个项目不同的任务列表与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); } } }); ...

更多推荐

只有最后一个任务运行!

本文发布于:2023-11-13 02:59:59,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1583239.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!