Wpf 任务不是';I don’我不是按顺序来的
我在任务方面遇到了一个小问题。。下面是我正在做的一个简单示例的屏幕截图。我希望第三份工作先出现,然后是第二份工作,然后是第一份工作(或者我做这件事的顺序) 我单击下拉框,它会将任务添加到listbox1。。。然后我按下按钮,它根据(任务)列表执行这些任务 我在第三个作业上有1000毫秒的延迟,所以当然,它是最后一个。我该怎么做才能让他们按照我想要的顺序来,不管需要多长时间 我正在使用的当前代码Wpf 任务不是';I don’我不是按顺序来的,wpf,vb.net,Wpf,Vb.net,我在任务方面遇到了一个小问题。。下面是我正在做的一个简单示例的屏幕截图。我希望第三份工作先出现,然后是第二份工作,然后是第一份工作(或者我做这件事的顺序) 我单击下拉框,它会将任务添加到listbox1。。。然后我按下按钮,它根据(任务)列表执行这些任务 我在第三个作业上有1000毫秒的延迟,所以当然,它是最后一个。我该怎么做才能让他们按照我想要的顺序来,不管需要多长时间 我正在使用的当前代码 Dim tasklist As New List(Of Task) Private Async S
Dim tasklist As New List(Of Task)
Private Async Sub Button_Click(sender As Object, e As RoutedEventArgs)
For Each item In tasklist
'item.RunSynchronously()
item.Start()
Next
Await Task.WhenAll(tasklist.ToArray)
End Sub
Public Sub FirstJob()
Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Background, DirectCast(Function() listbox2.Items.Add("FirstJob"), Action))
End Sub
Public Sub SecondJob()
Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Background, DirectCast(Function() listbox2.Items.Add("SecondJob"), Action))
End Sub
Public Sub ThirdJob()
Thread.Sleep(1000)
Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Background, DirectCast(Function() listbox2.Items.Add("ThirdJob"), Action))
End Sub
Private Sub ComboBox_SelectionChanged(sender As Object, e As SelectionChangedEventArgs)
listbox1.Items.Add(cb1.SelectedItem)
If cb1.SelectedItem.ToString = "FirstJob" Then
tasklist.Add(New Task(Sub() FirstJob()))
ElseIf cb1.SelectedItem.ToString = "SecondJob" Then
tasklist.Add(New Task(Sub() SecondJob()))
ElseIf cb1.SelectedItem.ToString = "ThirdJob" Then
tasklist.Add(New Task(Sub() ThirdJob()))
End If
End Sub
Private Sub MainWindow_Loaded(sender As Object, e As RoutedEventArgs) Handles Me.Loaded
cb1.Items.Add("FirstJob")
cb1.Items.Add("SecondJob")
cb1.Items.Add("ThirdJob")
End Sub
我确信问题在于如何通过数组执行for循环
如果我改变
For Each item In tasklist
item.Start()
Next
Await Task.WhenAll(tasklist.ToArray)
到
它按顺序运行,但冻结了UI,这是我不想要的。有什么想法吗?创建一个自定义方法,然后使用
Task.ContinueWith()
方法依次启动任务如何
Public Class TaskHelper
Public Shared Function RunAllSequential(ByVal Tasks() As Task) As Task
For i = 0 To Tasks.Length - 2 'Using -2 since the last task don't need to start another one.
Dim j As Integer = i
Tasks(i).ContinueWith(Sub() Tasks(j + 1).Start())
Next
Tasks(0).Start() 'Start the first task.
Return Task.WhenAll(Tasks)
End Sub
End Class
这将依次运行指定的任务。第一次完成后,第二次将启动,依此类推
示例用法:
Await TaskHelper.RunAllSequential(tasklist.ToArray())
听起来你还需要一个后台工作人员如果你想让他们按顺序排列,你需要对
任务列表进行排序如果它是一个列表,那么就对它进行排序。。你有没有用调试器一步一步地调试代码并评估项
?下面是我最喜欢的一首歌中的一句话,可能会有帮助-“带我到并发城市,那里的绿色美女是草,女孩是草”.@Enigmativity聪明+1@TonyW:你想解决的实际问题是什么?@StephenCleary:在我看来,他希望按顺序从任务中获得输出。请注意,我是在手机上写的;我还没有测试过它。所以,如果你遇到任何问题,请告诉我。我一回家就会检查,谢谢。它可能是一个包含2个或3个,或20个的列表。我想我只需要得到列表的长度,并除去除初始项以外的所有内容,因此它是先完成的,除此之外的所有内容都将继续。@TonyW:不,您不需要除去任何内容。只需像上面那样将列表转换为数组。我的代码将:1)设置每个任务(最后一个任务除外)ContinueWith()
以启动下一个任务。2) 只启动第一个任务。3) 使用数组作为参数调用Task.WhenAll()
,并返回结果任务。-您应该可以复制粘贴我的类,并在代码中将wait Task.WhenAll(tasklist.ToArray)
更改为wait taskheloper.RunAllSequential(tasklist.ToArray())
@TonyW。任务不可能多次运行。因此,如果要运行此方法两次,必须创建通过参数Tasks()
提供的新任务对象。有关任务的更多信息,请参阅文章(问题2)。有人愿意详细说明否决票,还是跟踪我几个月的那个仇恨者?
Await TaskHelper.RunAllSequential(tasklist.ToArray())