任务没有按照我需要的顺序来 [英] Tasks aren't coming in the order I need them to

查看:25
本文介绍了任务没有按照我需要的顺序来的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在任务方面遇到了一个小问题.. 下面是我正在做的一个简单示例的屏幕截图.我希望第 三份工作首先出现,然后是第二份工作,然后是第一份工作(或我正在做的任何顺序).

I'm having a small issue with tasks.. below is a screenshot of a simple example of what I'm doing. I want the ThirdJob to show up first, then SecondJob, then FirstJob (or whatever order I'm doing it in).

我单击下拉框,它将任务添加到 listbox1...然后我按下按钮,它会根据(任务列表)执行这些任务.

I click the dropdown box, and it adds tasks to listbox1... then I press the button, and it executes those tasks based on a list(of task).

我在 ThirdJob 上有 1000 毫秒的延迟,所以当然,它排在最后.我如何做到让它们按照我希望的顺序出现,无论需要多长时间.

I have a 1000 MS delay on the ThirdJob, so of course, it comes in last. How do I make it so they come in the order I want them to, no matter how long it takes.

我用来执行此操作的当前代码..

Current code I'm using to do this..

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 循环.

I'm sure the issue lies in how I'm doing my for loop though my array.

如果我改变了

For Each item In tasklist
   item.Start()
Next

Await Task.WhenAll(tasklist.ToArray)

For Each item In tasklist
   item.Start()
   item.wait
Next

它按顺序进行,但冻结了 UI,这是我不想要的.有什么想法吗?

It goes in order, but freezes the UI, which I don't want. Any ideas?

推荐答案

如何创建自定义方法并利用 Task.ContinueWith() 方法依次启动任务?

How about creating a custom method and utilizing the Task.ContinueWith() method to start the tasks after each other?

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

这将依次运行指定的任务.第一个完成后,第二个将开始,依此类推.

This will run the specified tasks after each other. When the first one is done the second one will be started, and so on.

示例使用:

Await TaskHelper.RunAllSequential(tasklist.ToArray())

这篇关于任务没有按照我需要的顺序来的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆