Quote:
Something seems to be wrong
This is like saying "My car won't start". We're like "did he turn the key, what happened?". You need to be specific.
When you ran the code did you get a thread exception error? That would be my first guess as you're doing unsafe thread operations.
List
is not thread- safe and
entriesTasks.Remove(finishedTask)
will be the culprit. You are better off using a
ConcurrentBag
and use
TryTake
to remove entries.
A rewrite would be:
Dim entriesTasksQuery = From fi In LTODateilisten Select New Task(Of IEnumerable(Of FileData))(Function()
Dim dlf As New DirListFile(fi)
Return dlf.ReadFileEntriesAsync(fi)
End Function)
Dim entriesTasks As New ConcurrentBag(Of Task(Of IEnumerable(Of FileData)))(entriesTasksQuery)
While entriesTasks.Count > 0
Dim finishedTask As Task(Of IEnumerable(Of FileData)) = Await Task.WhenAny(entriesTasks)
entriesTasks.TryTake(finishedTask)
Dim entries As IEnumerable(Of FileData) = Await finishedTask
Dim msg As String = $"{entries.Count} entries found."
Debug.WriteLine(msg)
End While
This is untested but will fix the thread exception. But without you explaining in more detail, I am only guessing.
UPDATE
To force break on exceptions, go Debug > Windows > Exception Settings > click on "Common Language Runtime Exceptions" (must be a tick). This should catch it.
Update
Just did a quick sample and this works fine:
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Threading.Tasks
Module Program
Sub Main()
Dim Tasks As New List(Of Task)()
For i As Integer = 0 To 99
Dim task As New DummyTask()
Tasks.Add(task.ExecuteAsync())
Next
Console.WriteLine($"Running Tasks: {Tasks.Count}")
While Tasks.Count > 0
Dim completed As List(Of Task) = Tasks.Where(Function(task) task.IsCompleted).ToList()
If completed.Any() = True Then
For Each task As Task In completed.ToList()
Tasks.Remove(task)
Console.WriteLine($"Running Tasks: {Tasks.Count}")
Next
End If
End While
Console.ReadLine()
End Sub
End Module
Public Class DummyTask
Private random As New Random()
Public Async Function ExecuteAsync() As Task
Await Task.Delay(random.Next(3000, 5000))
End Function
End Class
and the output:
Running Tasks: 100
Running Tasks: 99
Running Tasks: 98
Running Tasks: 97
Running Tasks: 96
Running Tasks: 95
Running Tasks: 94
Running Tasks: 93
Running Tasks: 92
Running Tasks: 91
Running Tasks: 90
Running Tasks: 89
Running Tasks: 88
Running Tasks: 87
Running Tasks: 86
Running Tasks: 85
Running Tasks: 84
Running Tasks: 83
Running Tasks: 82
Running Tasks: 81
Running Tasks: 80
Running Tasks: 79
Running Tasks: 78
Running Tasks: 77
Running Tasks: 76
Running Tasks: 75
Running Tasks: 74
Running Tasks: 73
Running Tasks: 72
Running Tasks: 71
Running Tasks: 70
Running Tasks: 69
Running Tasks: 68
Running Tasks: 67
Running Tasks: 66
Running Tasks: 65
Running Tasks: 64
Running Tasks: 63
Running Tasks: 62
Running Tasks: 61
Running Tasks: 60
Running Tasks: 59
Running Tasks: 58
Running Tasks: 57
Running Tasks: 56
Running Tasks: 55
Running Tasks: 54
Running Tasks: 53
Running Tasks: 52
Running Tasks: 51
Running Tasks: 50
Running Tasks: 49
Running Tasks: 48
Running Tasks: 47
Running Tasks: 46
Running Tasks: 45
Running Tasks: 44
Running Tasks: 43
Running Tasks: 42
Running Tasks: 41
Running Tasks: 40
Running Tasks: 39
Running Tasks: 38
Running Tasks: 37
Running Tasks: 36
Running Tasks: 35
Running Tasks: 34
Running Tasks: 33
Running Tasks: 32
Running Tasks: 31
Running Tasks: 30
Running Tasks: 29
Running Tasks: 28
Running Tasks: 27
Running Tasks: 26
Running Tasks: 25
Running Tasks: 24
Running Tasks: 23
Running Tasks: 22
Running Tasks: 21
Running Tasks: 20
Running Tasks: 19
Running Tasks: 18
Running Tasks: 17
Running Tasks: 16
Running Tasks: 15
Running Tasks: 14
Running Tasks: 13
Running Tasks: 12
Running Tasks: 11
Running Tasks: 10
Running Tasks: 9
Running Tasks: 8
Running Tasks: 7
Running Tasks: 6
Running Tasks: 5
Running Tasks: 4
Running Tasks: 3
Running Tasks: 2
Running Tasks: 1
Running Tasks: 0
Everything works. However, if you have thread exception in the wait loop, refer to my first comment.