0

I have been struggling for the last few hours to get a basic implementation of BlockingCollection to work. I cannot seem to find a tutorial in VB.NET for the life of me, so have been trying to get something working from tutorials in C#.

I just want to get a basic implementation working so I can progress to doing what I actually need. But I am stuck.

This is my code currently:

Dim blockingCollection As BlockingCollection(Of String) = New BlockingCollection(Of String)
        Dim count As Integer = 0

        Task.Factory.StartNew(Sub()
                                  While (True)
                                      blockingCollection.Add("value" + count)
                                      count = count + 1
                                  End While
                              End Sub)

        Task.Factory.StartNew(Sub()
                                  For Each value As String In blockingCollection.GetConsumingEnumerable()
                                      Debug.Print("Worker 1: " + value)
                                  Next
                              End Sub)

I get no output from running this, just the following exceptions:

A first chance exception of type 'System.FormatException' occurred in mscorlib.dll A first chance exception of type 'System.FormatException' occurred in Microsoft.VisualBasic.dll A first chance exception of type 'System.InvalidCastException' occurred in Microsoft.VisualBasic.dll

I have been googling persistently for the past few hours to try and resolve this, but to no avail. Hopefully someone here can help.

Thanks

Henry Ing-Simmons
  • 1,362
  • 5
  • 18
  • 25
  • 1
    Put Option Strict On at the top of the source code file and the compiler will tell you about the problem. Avoid adding a string and a number, use count.ToString(). Also shows you that it is important to *check* if a task completed successfully. Use ContinueWith() with TaskContinuationOptions.OnlyOnFaulted to report exceptions. – Hans Passant Mar 15 '14 at 12:46

1 Answers1

1

This code executes, though I don't know what it is that you want.

    Dim blockingCollection As BlockingCollection(Of String) = New BlockingCollection(Of String)
    Dim count As Integer = 0

    Task.Factory.StartNew(Sub()
                              While True
                                  blockingCollection.Add("value" & count.ToString)
                                  count = count + 1
                              End While
                          End Sub)

    Task.Factory.StartNew(Sub()
                              For Each value As String In blockingCollection.GetConsumingEnumerable()
                                  Debug.WriteLine("Worker 1: " & value)
                              Next
                          End Sub)
dbasnett
  • 11,334
  • 2
  • 25
  • 33
  • All I want is a basic working example to understand it before moving on. I get the same problems with all the tutorials I have tried – Henry Ing-Simmons Mar 16 '14 at 01:05
  • Thanks. Sorry I was working into the early hours and have been flitting between languages. Forget in my converting from C# tutorial to vb.net code that VB is awkwardly different to every other programming language. Problems with + rather than & – Henry Ing-Simmons Mar 16 '14 at 01:13