Imports System.ComponentModel

Public Class VStest1Form

    ' 23-12-2010   First Release
    ' 28-12-2010   Added sub init

    Public x1(10, 10), y1(10, 10), z1(10, 10)
    Public x2(10, 10), y2(10, 10), z2(10, 10)
    Public x3(10, 10), y3(10, 10), z3(10, 10)
    Public x4(10, 10), y4(10, 10), z4(10, 10)
    Public itercnt(4), seccnt(4), totalp(4) As Integer
    Public np, lnp As Integer
    Public load1 As Integer

    Public Sub New()
        InitializeComponent()
        InitForm()
        BackgroundWorkerpp1.WorkerReportsProgress = True
        BackgroundWorkerpp1.WorkerSupportsCancellation = True
        BackgroundWorkerpp2.WorkerReportsProgress = True
        BackgroundWorkerpp2.WorkerSupportsCancellation = True
        BackgroundWorkerpp3.WorkerReportsProgress = True
        BackgroundWorkerpp3.WorkerSupportsCancellation = True
        BackgroundWorkerpp4.WorkerReportsProgress = True
        BackgroundWorkerpp4.WorkerSupportsCancellation = True
        ResultLabelpp1.Text = " "
        ResultLabelpp2.Text = " "
        ResultLabelpp3.Text = " "
        ResultLabelpp4.Text = " "
    End Sub

    Private Sub startAsyncButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StartAsyncButton.Click

        Dim a As String

        If BackgroundWorkerpp1.IsBusy <> True Then
            ' Start the asynchronous operation.
            BackgroundWorkerpp1.RunWorkerAsync()
            np = 1
            ProcTpp1.Text = 1
        Else
            If BackgroundWorkerpp2.IsBusy <> True Then
                ' Start the asynchronous operation.
                BackgroundWorkerpp2.RunWorkerAsync()
                np = 2
                ProcTpp2.Text = 2
            Else
                If BackgroundWorkerpp3.IsBusy <> True Then
                    ' Start the asynchronous operation.
                    BackgroundWorkerpp3.RunWorkerAsync()
                    np = 3
                    ProcTpp3.Text = 3
                Else
                    If BackgroundWorkerpp4.IsBusy <> True Then
                        ' Start the asynchronous operation.
                        BackgroundWorkerpp4.RunWorkerAsync()
                        np = 4
                        ProcTpp4.Text = 4
                    End If

                End If

            End If

        End If
        nProcT.Text = np
        For i = 0 To 4
            itercnt(i) = -1 : seccnt(i) = -1
        Next

        a = LoadT.Text : load1 = Val(a)       ' Load loop counter
        If load1 = 0 Then load1 = 1
    End Sub

    Private Sub cancelAsyncButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CancelAsyncButton.Click

        Dim a As String

        If BackgroundWorkerpp1.WorkerSupportsCancellation = True And np = 1 Then
            ' Cancel the asynchronous operation.
            BackgroundWorkerpp1.CancelAsync()
            np = 0
            ProcTpp1.Text = " "
        End If
        If BackgroundWorkerpp2.WorkerSupportsCancellation = True And np = 2 Then
            ' Cancel the asynchronous operation.
            BackgroundWorkerpp2.CancelAsync()
            np = 1
            ProcTpp2.Text = " "
        End If
        If BackgroundWorkerpp3.WorkerSupportsCancellation = True And np = 3 Then
            ' Cancel the asynchronous operation.
            BackgroundWorkerpp3.CancelAsync()
            ProcTpp3.Text = " "
            np = 2
        End If
        If BackgroundWorkerpp4.WorkerSupportsCancellation = True And np = 4 Then
            ' Cancel the asynchronous operation.
            BackgroundWorkerpp4.CancelAsync()
            np = 3
            ProcTpp4.Text = " "
        End If
        nProcT.Text = np
        For i = 0 To 4
            itercnt(i) = -1 : seccnt(i) = -1
        Next
        a = LoadT.Text : load1 = Val(a)       ' Load loop counter
        If load1 = 0 Then load1 = 1

    End Sub


    Private Sub EndSyncButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EndAsyncButton.Click

        Debug.Print("Click End")

        End
    End Sub


    ' This event handler 1 is where the time-consuming work is done.
    Private Sub backgroundWorkerpp1_DoWork(ByVal sender As System.Object, _
    ByVal e As DoWorkEventArgs) Handles BackgroundWorkerpp1.DoWork
        Dim worker As BackgroundWorker = CType(sender, BackgroundWorker)
        Dim i As Integer
        Dim sec1, sec2 As Integer
        Dim a As String
        sec1 = Second(Now)
        a = LoadT.Text : load1 = Val(a)       ' Load loop counter
        If load1 = 0 Then load1 = 1
        Debug.Print("PP1 Load " + Str(load1))
        Do
            itercnt(1) = itercnt(1) + 1
            For ip = 1 To load1
                For i = 0 To 10
                    For j = 0 To 10
                        x1(i, j) = y1(i, j) + z1(i, j)
                    Next
                Next
            Next ip

            If (worker.CancellationPending = True) Then
                e.Cancel = True
                totalp(1) = 0
                Exit Sub
            Else
                ' Perform a time consuming operation and report progress.
                sec2 = Second(Now)
                If sec2 <> sec1 Then
                    System.Threading.Thread.Sleep(1)
                    sec1 = Second(Now)
                    seccnt(1) = seccnt(1) + 1
                    If seccnt(1) <= 0 Then
                        itercnt(1) = 0
                        worker.ReportProgress(0)
                    Else
                        totalp(1) = itercnt(1) / seccnt(1)
                        worker.ReportProgress(totalp(1))
                    End If
                End If
            End If
        Loop

    End Sub

    ' This event handler 1 updates the progress.
    Private Sub backgroundWorkerpp1_ProgressChanged(ByVal sender As System.Object, _
    ByVal e As ProgressChangedEventArgs) Handles BackgroundWorkerpp1.ProgressChanged
        ResultLabelpp1.Text = (e.ProgressPercentage.ToString())
        TotalL.Text = Str(totalp(1) + totalp(2) + totalp(3) + totalp(4))
        seccntT.Text = Str(seccnt(1))
    End Sub

    ' This event handler 1  deals with the results of the background operation.
    Private Sub backgroundWorkerpp1_RunWorkerCompleted(ByVal sender As System.Object, _
    ByVal e As RunWorkerCompletedEventArgs) Handles BackgroundWorkerpp1.RunWorkerCompleted
        If e.Cancelled = True Then
            ResultLabelpp1.Text = "Canceled!"
        ElseIf e.Error IsNot Nothing Then
            ResultLabelpp1.Text = "Error: " & e.Error.Message
        Else
            ResultLabelpp1.Text = "Done!"
        End If
    End Sub

    ' This event handler 2 is where the time-consuming work is done.
    Private Sub backgroundWorkerpp2_DoWork(ByVal sender As System.Object, ByVal e As DoWorkEventArgs) Handles BackgroundWorkerpp2.DoWork
        Dim worker As BackgroundWorker = CType(sender, BackgroundWorker)
        Dim i As Integer
        Dim sec1 As Integer : Dim sec2 As Integer
        sec1 = Second(Now)
        Do
            itercnt(2) = itercnt(2) + 1
            For ip = 1 To load1
                For i = 0 To 10
                    For j = 0 To 10
                        x2(i, j) = y2(i, j) + z2(i, j)
                    Next
                Next
            Next ip

            If (worker.CancellationPending = True) Then
                e.Cancel = True
                totalp(2) = 0
                Exit Sub
            Else
                ' Perform a time consuming operation and report progress.
                sec2 = Second(Now)
                If sec2 <> sec1 Then
                    System.Threading.Thread.Sleep(1)
                    sec1 = Second(Now)
                    seccnt(2) = seccnt(2) + 1
                    If seccnt(2) <= 0 Then
                        itercnt(2) = 0
                        worker.ReportProgress(0)
                    Else
                        totalp(2) = itercnt(2) / seccnt(2)
                        worker.ReportProgress(totalp(2))
                    End If
                End If
            End If
        Loop

    End Sub

    ' This event handler 2 updates the progress.
    Private Sub backgroundWorkerpp2_ProgressChanged(ByVal sender As System.Object, _
    ByVal e As ProgressChangedEventArgs) Handles BackgroundWorkerpp2.ProgressChanged
        ResultLabelpp2.Text = (e.ProgressPercentage.ToString())
    End Sub

    ' This event handler 2 deals with the results of the background operation.
    Private Sub backgroundWorkerpp2_RunWorkerCompleted(ByVal sender As System.Object, _
    ByVal e As RunWorkerCompletedEventArgs) Handles BackgroundWorkerpp2.RunWorkerCompleted
        If e.Cancelled = True Then
            ResultLabelpp2.Text = "Canceled!"
        ElseIf e.Error IsNot Nothing Then
            ResultLabelpp2.Text = "Error: " & e.Error.Message
        Else
            ResultLabelpp2.Text = "Done!"
        End If
    End Sub

    ' This event handler 3 is where the time-consuming work is done.
    Private Sub backgroundWorkerpp3_DoWork(ByVal sender As System.Object, _
    ByVal e As DoWorkEventArgs) Handles BackgroundWorkerpp3.DoWork
        Dim worker As BackgroundWorker = CType(sender, BackgroundWorker)
        Dim i As Integer
        Dim sec1 As Integer : Dim sec2 As Integer
        sec1 = Second(Now)
        Do
            itercnt(3) = itercnt(3) + 1
            For ip = 1 To load1
                For i = 0 To 10
                    For j = 0 To 10
                        x3(i, j) = y3(i, j) + z3(i, j)
                    Next
                Next
            Next ip

            If (worker.CancellationPending = True) Then
                e.Cancel = True
                totalp(3) = 0
                Exit Sub
            Else
                ' Perform a time consuming operation and report progress.
                sec2 = Second(Now)
                If sec2 <> sec1 Then
                    System.Threading.Thread.Sleep(1)
                    sec1 = Second(Now)
                    seccnt(3) = seccnt(3) + 1
                    If seccnt(3) <= 0 Then
                        itercnt(3) = 0
                        worker.ReportProgress(0)
                    Else
                        totalp(3) = itercnt(3) / seccnt(3)
                        worker.ReportProgress(totalp(3))
                    End If
                End If
            End If
        Loop

    End Sub

    ' This event handler 3 updates the progress.
    Private Sub backgroundWorkerpp3_ProgressChanged(ByVal sender As System.Object, _
    ByVal e As ProgressChangedEventArgs) Handles BackgroundWorkerpp3.ProgressChanged
        ResultLabelpp3.Text = (e.ProgressPercentage.ToString())
    End Sub

    ' This event handler 3 deals with the results of the background operation.
    Private Sub backgroundWorkerpp3_RunWorkerCompleted(ByVal sender As System.Object, _
    ByVal e As RunWorkerCompletedEventArgs) Handles BackgroundWorkerpp3.RunWorkerCompleted
        If e.Cancelled = True Then
            ResultLabelpp3.Text = "Canceled!"
        ElseIf e.Error IsNot Nothing Then
            ResultLabelpp3.Text = "Error: " & e.Error.Message
        Else
            ResultLabelpp3.Text = "Done!"
        End If
    End Sub

    ' This event handler 4 is where the time-consuming work is done.
    Private Sub backgroundWorkerpp4_DoWork(ByVal sender As System.Object, ByVal e As DoWorkEventArgs) Handles BackgroundWorkerpp4.DoWork
        Dim worker As BackgroundWorker = CType(sender, BackgroundWorker)
        Dim i As Integer
        Dim sec1 As Integer : Dim sec2 As Integer
        sec1 = Second(Now)
        Do
            itercnt(4) = itercnt(4) + 1
            For ip = 1 To load1
                For i = 0 To 10
                    For j = 0 To 10
                        x4(i, j) = y4(i, j) + z4(i, j)
                    Next
                Next
            Next ip

            If (worker.CancellationPending = True) Then
                e.Cancel = True
                totalp(4) = 0
                Exit Sub
            Else
                ' Perform a time consuming operation and report progress.
                sec2 = Second(Now)
                If sec2 <> sec1 Then
                    System.Threading.Thread.Sleep(1)
                    sec1 = Second(Now)
                    seccnt(4) = seccnt(4) + 1
                    If seccnt(4) <= 0 Then
                        itercnt(4) = 0
                        worker.ReportProgress(0)
                    Else
                        totalp(4) = itercnt(4) / seccnt(4)
                        worker.ReportProgress(totalp(4))
                    End If
                End If
            End If
        Loop

    End Sub

    ' This event handler 4 updates the progress.
    Private Sub backgroundWorkerpp4_ProgressChanged(ByVal sender As System.Object, _
    ByVal e As ProgressChangedEventArgs) Handles BackgroundWorkerpp4.ProgressChanged
        ResultLabelpp4.Text = (e.ProgressPercentage.ToString())
    End Sub

    ' This event handler 4 deals with the results of the background operation.
    Private Sub backgroundWorkerpp4_RunWorkerCompleted(ByVal sender As System.Object, _
    ByVal e As RunWorkerCompletedEventArgs) Handles BackgroundWorkerpp4.RunWorkerCompleted
        If e.Cancelled = True Then
            ResultLabelpp4.Text = "Canceled!"
        ElseIf e.Error IsNot Nothing Then
            ResultLabelpp4.Text = "Error: " & e.Error.Message
        Else
            ResultLabelpp4.Text = "Done!"
        End If
    End Sub

    Sub Init()

        For i = 0 To 10
            For j = 0 To 10
                y1(i, j) = (i + 1) / (j + 1) : z1(i, j) = 3.14 / y1(i, j)
                y2(i, j) = (i + 1) / (j + 1) : z2(i, j) = 3.14 / y2(i, j)
                y3(i, j) = (i + 1) / (j + 1) : z3(i, j) = 3.14 / y3(i, j)
                y4(i, j) = (i + 1) / (j + 1) : z4(i, j) = 3.14 / y4(i, j)
            Next
        Next

    End Sub


    Sub InitForm()
        Dim hh, dhh As Integer
        Dim dd, ddd As Integer
        '
        'Row  1
        '
        hh = 12 : dhh = 34
        Me.procnL.BorderStyle = System.Windows.Forms.BorderStyle.None
        Me.procnL.Location = New System.Drawing.Point(3, hh)
        Me.procnL.Name = "ProcnL"
        Me.procnL.Size = New System.Drawing.Size(39, 13)
        Me.procnL.TabIndex = 10
        Me.procnL.Text = "Proc #"
        Me.procnL.TextAlign = System.Drawing.ContentAlignment.TopLeft

        dd = 60 : ddd = 70
        Me.ProcTpp1.Location = New System.Drawing.Point(dd, hh)
        Me.ProcTpp1.Name = "ProcTpp1"
        Me.ProcTpp1.Size = New System.Drawing.Size(64, 20)
        Me.ProcTpp1.TabIndex = 11
        Me.ProcTpp1.Text = ""

        dd = dd + ddd
        Me.ProcTpp2.Location = New System.Drawing.Point(dd, hh)
        Me.ProcTpp2.Name = "ProcTpp2"
        Me.ProcTpp2.Size = New System.Drawing.Size(64, 20)
        Me.ProcTpp2.TabIndex = 12
        Me.ProcTpp2.Text = ""

        dd = dd + ddd
        Me.ProcTpp3.Location = New System.Drawing.Point(dd, hh)
        Me.ProcTpp3.Name = "ProcTpp3"
        Me.ProcTpp3.Size = New System.Drawing.Size(64, 20)
        Me.ProcTpp3.TabIndex = 13
        Me.ProcTpp3.Text = ""

        dd = dd + ddd
        Me.ProcTpp4.Location = New System.Drawing.Point(dd, hh)
        Me.ProcTpp4.Name = "ProcTpp4"
        Me.ProcTpp4.Size = New System.Drawing.Size(64, 20)
        Me.ProcTpp4.TabIndex = 14
        Me.ProcTpp4.Text = ""

        dd = dd + ddd
        Me.TotalT.Location = New System.Drawing.Point(dd, hh)
        Me.TotalT.Name = "TotalT"
        Me.TotalT.Size = New System.Drawing.Size(64, 20)
        Me.TotalT.TabIndex = 15
        Me.TotalT.Text = "Total"
        '
        'Row 2
        '
        hh = hh + dhh
        Me.Label2.BorderStyle = System.Windows.Forms.BorderStyle.None
        Me.Label2.Location = New System.Drawing.Point(3, hh)
        Me.Label2.Name = "Label2"
        Me.Label2.Size = New System.Drawing.Size(67, 13)
        Me.Label2.TabIndex = 16
        Me.Label2.Text = "Performance"
        Me.Label2.TextAlign = System.Drawing.ContentAlignment.TopLeft

        dd = 85
        Me.ResultLabelpp1.BorderStyle = System.Windows.Forms.BorderStyle.None
        Me.ResultLabelpp1.Location = New System.Drawing.Point(dd, hh)
        Me.ResultLabelpp1.Name = "ResultLabelpp1"
        Me.ResultLabelpp1.Size = New System.Drawing.Size(70, 13)
        Me.ResultLabelpp1.TabIndex = 3
        Me.ResultLabelpp1.Text = "pp1"
        Me.ResultLabelpp1.TextAlign = System.Drawing.ContentAlignment.TopLeft

        dd = dd + ddd
        Me.ResultLabelpp2.BorderStyle = System.Windows.Forms.BorderStyle.None
        Me.ResultLabelpp2.Location = New System.Drawing.Point(dd, hh)
        Me.ResultLabelpp2.Name = "ResultLabelpp2"
        Me.ResultLabelpp2.Size = New System.Drawing.Size(60, 13)
        Me.ResultLabelpp2.TabIndex = 3
        Me.ResultLabelpp2.Text = "pp2"
        Me.ResultLabelpp2.TextAlign = System.Drawing.ContentAlignment.TopLeft

        dd = dd + ddd
        Me.ResultLabelpp3.BorderStyle = System.Windows.Forms.BorderStyle.None
        Me.ResultLabelpp3.Location = New System.Drawing.Point(dd, hh)
        Me.ResultLabelpp3.Name = "ResultLabelpp3"
        Me.ResultLabelpp3.Size = New System.Drawing.Size(70, 13)
        Me.ResultLabelpp3.TabIndex = 3
        Me.ResultLabelpp3.Text = "pp3"
        Me.ResultLabelpp3.TextAlign = System.Drawing.ContentAlignment.TopLeft

        dd = dd + ddd
        Me.ResultLabelpp4.BorderStyle = System.Windows.Forms.BorderStyle.None
        Me.ResultLabelpp4.Location = New System.Drawing.Point(dd, hh)
        Me.ResultLabelpp4.Name = "ResultLabelpp4"
        Me.ResultLabelpp4.Size = New System.Drawing.Size(60, 13)
        Me.ResultLabelpp4.TabIndex = 3
        Me.ResultLabelpp4.Text = "pp4"
        Me.ResultLabelpp4.TextAlign = System.Drawing.ContentAlignment.TopLeft

        dd = dd + ddd
        Me.TotalL.BorderStyle = System.Windows.Forms.BorderStyle.None
        Me.TotalL.Location = New System.Drawing.Point(dd, hh)
        Me.TotalL.Name = "TotalL"
        Me.TotalL.Size = New System.Drawing.Size(70, 13)
        Me.TotalL.TabIndex = 3
        Me.TotalL.Text = "Total"
        Me.TotalL.TextAlign = System.Drawing.ContentAlignment.TopLeft
        '
        'Row  3
        '
        dd = 80 : ddd = 100
        hh = hh + dhh
        Me.nProcL.BorderStyle = System.Windows.Forms.BorderStyle.None
        Me.nProcL.Location = New System.Drawing.Point(dd - 40, hh)
        Me.nProcL.Name = "nProcL"
        Me.nProcL.Size = New System.Drawing.Size(55, 13)
        Me.nProcL.TabIndex = 3
        Me.nProcL.Text = "# Proc"
        Me.nProcL.TextAlign = System.Drawing.ContentAlignment.TopRight

        Me.NProcT.Location = New System.Drawing.Point(dd, hh)
        Me.NProcT.Name = "nProcT"
        Me.NProcT.Size = New System.Drawing.Size(40, 20)
        Me.NProcT.TabIndex = 3
        Me.NProcT.Text = "0"
        Me.NProcT.TextAlign = System.Drawing.ContentAlignment.TopLeft ' = right

        dd = dd + ddd
        Me.LoadL.BorderStyle = System.Windows.Forms.BorderStyle.None
        Me.LoadL.Location = New System.Drawing.Point(dd - 40, hh)
        Me.LoadL.Name = "LloadL"
        Me.LoadL.Size = New System.Drawing.Size(55, 13)
        Me.LoadL.TabIndex = 3
        Me.LoadL.Text = "Load"
        Me.LoadL.TextAlign = System.Drawing.ContentAlignment.TopRight

        Me.loadT.Location = New System.Drawing.Point(dd, hh)
        Me.loadT.Name = "loadT"
        Me.loadT.Size = New System.Drawing.Size(40, 20)
        Me.loadT.TabIndex = 3
        Me.LoadT.Text = "1"
        Me.loadT.TextAlign = System.Drawing.ContentAlignment.TopLeft ' = right

        dd = dd + ddd
        Me.seccntL.BorderStyle = System.Windows.Forms.BorderStyle.None
        Me.seccntL.Location = New System.Drawing.Point(dd - 40, hh)
        Me.seccntL.Name = "seccntL"
        Me.seccntL.Size = New System.Drawing.Size(55, 13)
        Me.seccntL.TabIndex = 3
        Me.seccntL.Text = "sec"
        Me.seccntL.TextAlign = System.Drawing.ContentAlignment.TopRight

        Me.seccntT.Location = New System.Drawing.Point(dd, hh)
        Me.seccntT.Name = "seccntT"
        Me.seccntT.Size = New System.Drawing.Size(40, 20)
        Me.seccntT.TabIndex = 3
        Me.seccntT.Text = "1"
        'Me.seccntT.TextAlign = System.Drawing.ContentAlignment.TopRight
        Me.seccntT.TextAlign = System.Drawing.ContentAlignment.TopLeft ' = right

        '
        ' row 4 startAsyncButton   cancelAsyncButton  endAsyncButton
        '
        dd = 60
        hh = hh + dhh
        Me.StartAsyncButton.Location = New System.Drawing.Point(dd, hh)
        Me.StartAsyncButton.Name = "startAsyncButton"
        Me.StartAsyncButton.Size = New System.Drawing.Size(63, 25)
        Me.StartAsyncButton.TabIndex = 0
        Me.StartAsyncButton.Text = "Start"

        dd = dd + ddd
        Me.CancelAsyncButton.Enabled = True
        Me.CancelAsyncButton.Location = New System.Drawing.Point(dd, hh)
        Me.CancelAsyncButton.Name = "cancelAsyncButton"
        Me.CancelAsyncButton.Size = New System.Drawing.Size(63, 25)
        Me.CancelAsyncButton.TabIndex = 1
        Me.CancelAsyncButton.Text = "Cancel"

        dd = dd + ddd
        Me.endAsyncButton.Location = New System.Drawing.Point(dd, hh)
        Me.endAsyncButton.Name = "EndAsyncButton"
        Me.endAsyncButton.Size = New System.Drawing.Size(63, 25)
        Me.endAsyncButton.TabIndex = 2
        Me.endAsyncButton.Text = "End"

        ' Form1
        dd = dd + ddd : hh = hh + dhh
        Me.ClientSize = New System.Drawing.Size(dd + 50, hh)

    End Sub




End Class