使我的多线程任务异步VB.net

时间:2017-10-06 20:51:32

标签: vb.net multithreading asynchronous task

所以我有一个循环遍历sub的任务,就像我的数组中有人一样。我知道制作这个Async会好很多,但我不确定这样做的最好方法。这是我的代码:

Private Sub status()
    Dim selectedStreamers As New List(Of String)
    For Each item In streamerList.CheckedItems
        selectedStreamers.Add(item)
    Next
    Dim tasks As New List(Of Task)()
    For Each streamerName In selectedStreamers
        Dim t As Task = Task.Run(Sub()
                                     streamerOnline(streamerName)
                                     Select Case condition
                                         Case StreamersCondition.Streaming
                                             Me.Invoke(Sub() console.Text &= streamerName + " is online." & vbNewLine)
                                         Case StreamersCondition.Offline
                                             Me.Invoke(Sub() console.Text &= streamerName + " is offline." & vbNewLine)
                                         Case StreamersCondition.UserNotFound
                                             Me.Invoke(Sub() console.Text &= streamerName + " hasn't been found." & vbNewLine)
                                     End Select
                                 End Sub)
        tasks.Add(t)
    Next
    Task.WaitAll(tasks.ToArray())
End Sub

此外,与手头的问题无关,但如果我想不断检查飘带是否流式传输。我会设置一个计时器,对吧?除了我在运行这个子程序时似乎无法正常工作?那么有没有什么好的方法可以让你每分钟不断检查一次(这样运行这个sub)?

0 个答案:

没有答案