Cannot Get Task Parts to Work as Intended

RogerSchlueter-7899 1,256 Reputation points
2022-05-04T20:56:57.66+00:00

I am trying to run a simulation in the TaskPool with updates to the UI thread. Here is what I've got so far:

Class MainWindow
    ...
    Private WithEvents Status As New Progress(Of Integer)

    Private Sub Run(sender As Object, e As RoutedEventArgs) Handles btnRun.Click
        <<Define CurrentScenario>>
        Dim cts As New CancellationTokenSource()
        Dim token As CancellationToken = cts.Token
        Task.Factory.StartNew(
            Sub()
                RunScenario(CurrentScenario, Status)
                token.ThrowIfCancellationRequested()
            End Sub)
        cts.Dispose()
    End Sub

    Private Shared Sub RunScenario(sc As Scenario, progress As IProgress(Of Integer))
        Iteration = 0          
        <<Perform iteration step>>
        Iteration += 1
        progress.Report(Iteration)
    End Sub

    Private Sub UIWriter(I As Integer) Handles Status.ProgressChanged
        txtRes.Text = CStr(I)
    End Sub

End Class

This gives a compile-timer error: Method 'UIWriter' cannot handle event 'ProgressChanged' because they do not have a compatible signature.

I do not see how those two are inconsisternt.

In addition, to make this work, I have to add "WithEvents" to the definition of Status. In all the examples of similar code, I have not seen this which leads me to suspect that something else is also wrong.

Help is appreciated.

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,663 questions
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 114.6K Reputation points
    2022-05-04T21:06:02.683+00:00

    Try this modification: Private Sub UIWriter(Sender as Object, I As Integer) Handles Status.ProgressChanged.


0 additional answers

Sort by: Most helpful