Imports System
Imports System.Collections
Imports System.ComponentModel
Imports System.Drawing
Imports System.Threading
Imports System.Windows.Forms
Imports System.Math


Public Class PlanetPPForm
    Inherits System.Windows.Forms.Form

    Public x(idim), y(idim), z(idim)
    Public vx(idim), vy(idim), vz(idim)
    Public ax(idim), ay(idim), az(idim)
    Public Rev(idim) As Integer              ' revolution counter
    Public pl_itercnt(idim) As Integer       ' iteration counter per planet
    Public m(idim)
    Public itercnt(npmax), seccnt(npmax)
    Public perfs(npmax) As String
    Public np, nplanets, holdreq, holdrev, cancelreq As Integer
    Public state(npmax) As Integer
    Const StartSt As Integer = 1, ActiveSt As Integer = 2, StopSt As Integer = 0, CancelSt As Integer = 3, Endst = 0
    Const StartSt1 As Integer = 4, Holdst As Integer = 5
    Const idim As Integer = 100
    Const npmax As Integer = 5
    Const Trace As Integer = 0
    ' Public Sleepc As Integer
    Public dt As Single              ' delta t
    Public errorc As Integer

    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()
        InitForm()
        ' Add any initialization after the InitializeComponent() call.

    End Sub

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

        For i = 0 To 5
            perfs(i) = " "
        Next

        StartAsyncButton.Enabled = False
        CancelAsyncButton.Enabled = True
        CancelAsyncButton.Text = "Stop"
        holdrev = HoldT.Value
        nProcT.Enabled = False
        tdeltaT.Enabled = False
        nplT.Enabled = False
        HoldT.Enabled = False

        Master()                   '  Master

    End Sub

    Sub Master()

        Dim npreq As Single
        Dim i, j, pstr As Integer
        Dim sec1, sec2 As Integer
        Dim dx, dy, dz, r2, r, a, ynew As Double
        Dim b As String
        Const pp = 1
        npreq = 0

        sec1 = Second(Now)
        pstr = 0                            ' Processor start = 1-1
        holdreq = 0
        b = tdeltaT.Text : dt = Val(b)      ' Delta t
        If dt = 0 Then dt = 0.02
        b = nplT.Value : nplanets = Val(b)
        If nplanets < 1 Then nplanets = 1
        If nplanets > idim Then nplanets = idim

        If Trace = 1 Then Debug.Print("PP " + Str(1) + "Load  dt = " + Str(dt) + " nplanets" + Str(nplanets))

        Init()

        Do
            npreq = nProcT.Value     'requested value
            If cancelreq = 1 Then npreq = 0
            If np <> npreq Then
                Assign(npreq)
                For i = 0 To npmax
                    itercnt(i) = 0 : seccnt(i) = -1
                Next
            End If
            If Rev(1) = holdrev Or holdreq = 2 Then
                holdrev = -1
                holdreq = 1
                For i = 1 To npmax
                    state(i) = Holdst
                Next i
                DisplayP()
                nProcT.Enabled = True
                CancelAsyncButton.Enabled = True
                CancelAsyncButton.Text = "Restart"
                HoldT.Enabled = True
            End If
            If state(pp) = Holdst Then
                If holdreq = 0 Then        ' Restart
                    For i = 1 To npmax
                        state(i) = Endst
                    Next
                Else
                    System.Threading.Thread.Sleep(1)
                    Application.DoEvents()              'required
                End If
            Else
                If state(pp) = CancelSt Then
                    Test()
                    perfs(pp) = "Canceled!"
                    state(1) = Endst
                    np = 0
                    If Trace = 1 Then Debug.Print("Master Cancelst ")
                    Exit Sub
                Else
                    If np = 1 Then Application.DoEvents()
                    itercnt(pp) = itercnt(pp) + 1
                    If np > 1 Then state(2) = StartSt ' Start P1
                    If np > 2 Then state(3) = StartSt ' start p2
                    If np > 3 Then state(4) = StartSt ' Start P3
                    If np > 4 Then state(5) = StartSt ' Start P3
                    If np > 0 Then
                        For i = pstr To nplanets Step np
                            ax(i) = 0 : ay(i) = 0 : az(i) = 0
                            For j = 0 To nplanets
                                If i <> j Then
                                    dx = x(i) - x(j) : dy = y(i) - y(j) : dz = z(i) - z(j)
                                    r2 = (dx * dx + dy * dy + dz * dz) : r = Sqrt(r2)
                                    a = m(j) / (r2 * r)
                                    ax(i) = ax(i) + a * dx
                                    ay(i) = ay(i) + a * dy
                                    az(i) = az(i) + a * dz
                                End If
                            Next
                            vx(i) = vx(i) - ax(i) * dt
                            vy(i) = vy(i) - ay(i) * dt
                            vz(i) = vz(i) - az(i) * dt
                        Next
                    End If

                    If np > 1 Then
                        Do
                            Application.DoEvents()     ' Required in Publish mode
                        Loop Until state(2) = StopSt And state(3) = StopSt And state(4) = StopSt And state(5) = StopSt
                    End If

                    If np > 1 Then state(2) = StartSt1 ' Start P1
                    If np > 2 Then state(3) = StartSt1 ' start p2
                    If np > 3 Then state(4) = StartSt1 ' Start P3
                    If np > 4 Then state(5) = StartSt1 ' Start P3
                    If np > 0 Then
                        For i = pstr To nplanets Step np
                            x(i) = x(i) + vx(i) * dt
                            ynew = y(i) + vy(i) * dt
                            If y(i) < 0 And ynew > 0 Then Rev(i) = Rev(i) + 1
                            y(i) = ynew
                            z(i) = z(i) + vz(i) * dt
                            pl_itercnt(i) = pl_itercnt(i) + 1
                        Next
                    End If

                    If np > 1 Then
                        Do
                            Application.DoEvents()     ' Required in Publish mode
                        Loop Until state(2) = StopSt And state(3) = StopSt And state(4) = StopSt And state(5) = StopSt
                    End If

                    ' Perform a time consuming operation and report progress.
                    sec2 = Second(Now)
                    If sec2 <> sec1 Then
                        sec1 = Second(Now)
                        seccnt(pp) = seccnt(pp) + 1
                        If seccnt(pp) <= 0 Then
                            itercnt(pp) = 0
                            perfs(pp) = 0 : DisplayP()
                        Else
                            perfs(pp) = Int(itercnt(pp) / seccnt(pp)) : DisplayP()
                        End If
                    End If
                End If
            End If
        Loop


    End Sub


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

        Test()
        If errorc > 0 Then
            StateTpp1.Text = pl_itercnt(0)
            StateTpp2.Text = pl_itercnt(1)
            StateTpp3.Text = pl_itercnt(2)
            StateTpp4.Text = pl_itercnt(3)
            StateTpp5.Text = pl_itercnt(4)
        End If

        If CancelAsyncButton.Text = "Stop" Then
            holdreq = 2
            ' See Master
        Else
            CancelAsyncButton.Text = "Stop"
            EndAsyncButton.Enabled = True
            holdreq = 0      ' Restart
            holdrev = HoldT.Value
            If holdrev = Rev(1) Then holdrev = -1
            HoldT.Enabled = False
            For i = 0 To npmax
                itercnt(i) = 0 : seccnt(i) = -2
            Next
        End If


    End Sub

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

        Test()
        If errorc > 0 Then
            StateTpp1.Text = pl_itercnt(0)
            StateTpp2.Text = pl_itercnt(1)
            StateTpp3.Text = pl_itercnt(2)
            StateTpp4.Text = pl_itercnt(3)
            StateTpp5.Text = pl_itercnt(4)
        End If

        If np <> 0 Then
            StartAsyncButton.Enabled = True
            CancelAsyncButton.Enabled = False
            tdeltaT.Enabled = True
            nplT.Enabled = True
            HoldT.Enabled = True
            cancelreq = 1
            holdreq = 0
        Else
            Debug.Print("Click End - no Error")
            End
        End If

    End Sub

    Private Sub MonitorButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MonitorButton.Click

        StateTpp1.Text = state(1)
        StateTpp2.Text = state(2)
        StateTpp3.Text = state(3)
        StateTpp4.Text = state(4)
        StateTpp5.Text = state(5)

    End Sub

    ' This event handler 1 is where the time-consuming work is done.
    Private Sub backgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As DoWorkEventArgs) Handles BackgroundWorker1.DoWork
        Dim worker As BackgroundWorker = CType(sender, BackgroundWorker)
        Dim i, j, pstr As Integer
        Dim sec1, sec2 As Integer
        Dim dx, dy, dz, r2, r, a, ynew As Double
        Const pp = 2              ' processor number or thread 
        sec1 = Second(Now)
        pstr = pp - 1
        If Trace = 1 Then Debug.Print("PP " + Str(pp) + " Load")
        Do
            itercnt(pp) = itercnt(pp) + 1
            Select Case state(pp)

                Case Is = Holdst
                    System.Threading.Thread.Sleep(1)

                Case Is = CancelSt
                    ' If (worker.CancellationPending = True) Or state(pp) = CancelSt Then
                    perfs(pp) = "Canceled!"
                    state(pp) = Endst
                    If Trace = 1 Then Debug.Print("PP " + Str(pp) + " Cancelst ")
                    Exit Sub

                Case Is = StartSt
                    state(pp) = ActiveSt
                    For i = pstr To nplanets Step np
                        ax(i) = 0 : ay(i) = 0 : az(i) = 0
                        For j = 0 To nplanets
                            If i <> j Then
                                dx = x(i) - x(j) : dy = y(i) - y(j) : dz = z(i) - z(j)
                                r2 = (dx * dx + dy * dy + dz * dz) : r = Sqrt(r2)
                                a = m(j) / (r2 * r)
                                ax(i) = ax(i) - a * dx
                                ay(i) = ay(i) - a * dy
                                az(i) = az(i) - a * dz
                            End If
                        Next
                        vx(i) = vx(i) + ax(i) * dt
                        vy(i) = vy(i) + ay(i) * dt
                        vz(i) = vz(i) + az(i) * dt
                    Next
                    state(pp) = StopSt

                Case Is = StartSt1
                    state(pp) = ActiveSt
                    For i = pstr To nplanets Step np
                        x(i) = x(i) + vx(i) * dt
                        ynew = y(i) + vy(i) * dt
                        If y(i) < 0 And ynew > 0 Then Rev(i) = Rev(i) + 1
                        y(i) = ynew
                        z(i) = z(i) + vz(i) * dt
                        pl_itercnt(i) = pl_itercnt(i) + 1
                    Next
                    state(pp) = StopSt

                    sec2 = Second(Now)
                    If sec2 <> sec1 Then
                        sec1 = Second(Now)
                        seccnt(pp) = seccnt(pp) + 1
                        If seccnt(pp) <= 0 Then
                            itercnt(pp) = 0
                            perfs(pp) = 0
                        Else
                            ' worker.ReportProgress(itercnt(pp) / seccnt(pp))
                            perfs(pp) = Int(itercnt(pp) / seccnt(pp))
                        End If
                    End If
            End Select
        Loop
    End Sub

    ' This event handler 2 is where the time-consuming work is done.
    Private Sub backgroundWorker2_DoWork(ByVal sender As System.Object, ByVal e As DoWorkEventArgs) Handles BackgroundWorker2.DoWork
        Dim worker As BackgroundWorker = CType(sender, BackgroundWorker)
        Dim i, j, pstr As Integer
        Dim sec1, sec2 As Integer
        Dim dx, dy, dz, r2, r, a, ynew As Double
        Const pp = 3              ' processor number or thread 
        sec1 = Second(Now)
        pstr = pp - 1
        If Trace = 1 Then Debug.Print("PP " + Str(pp) + " Load")
        Do
            itercnt(pp) = itercnt(pp) + 1
            Select Case state(pp)

                Case Is = Holdst
                    System.Threading.Thread.Sleep(1)

                Case Is = CancelSt
                    ' If (worker.CancellationPending = True) Or state(pp) = CancelSt Then
                    perfs(pp) = "Canceled!"
                    state(pp) = Endst
                    If Trace = 1 Then Debug.Print("PP " + Str(pp) + " Cancelst ")
                    Exit Sub

                Case Is = StartSt
                    state(pp) = ActiveSt
                    For i = pstr To nplanets Step np
                        ax(i) = 0 : ay(i) = 0 : az(i) = 0
                        For j = 0 To nplanets
                            If i <> j Then
                                dx = x(i) - x(j) : dy = y(i) - y(j) : dz = z(i) - z(j)
                                r2 = (dx * dx + dy * dy + dz * dz) : r = Sqrt(r2)
                                a = m(j) / (r2 * r)
                                ax(i) = ax(i) - a * dx
                                ay(i) = ay(i) - a * dy
                                az(i) = az(i) - a * dz
                            End If
                        Next
                        vx(i) = vx(i) + ax(i) * dt
                        vy(i) = vy(i) + ay(i) * dt
                        vz(i) = vz(i) + az(i) * dt
                    Next
                    state(pp) = StopSt

                Case Is = StartSt1
                    state(pp) = ActiveSt
                    For i = pstr To nplanets Step np
                        x(i) = x(i) + vx(i) * dt
                        ynew = y(i) + vy(i) * dt
                        If y(i) < 0 And ynew > 0 Then Rev(i) = Rev(i) + 1
                        y(i) = ynew
                        z(i) = z(i) + vz(i) * dt
                        pl_itercnt(i) = pl_itercnt(i) + 1
                    Next
                    state(pp) = StopSt

                    sec2 = Second(Now)
                    If sec2 <> sec1 Then
                        sec1 = Second(Now)
                        seccnt(pp) = seccnt(pp) + 1
                        If seccnt(pp) <= 0 Then
                            itercnt(pp) = 0
                            perfs(pp) = 0
                        Else
                            ' worker.ReportProgress(itercnt(pp) / seccnt(pp))
                            perfs(pp) = Int(itercnt(pp) / seccnt(pp))
                        End If
                    End If
            End Select
        Loop
    End Sub


    ' This event handler 3 is where the time-consuming work is done.
    Private Sub backgroundWorker3_DoWork(ByVal sender As System.Object, ByVal e As DoWorkEventArgs) Handles BackgroundWorker3.DoWork
        Dim worker As BackgroundWorker = CType(sender, BackgroundWorker)
        Dim i, j, pstr As Integer
        Dim sec1, sec2 As Integer
        Dim dx, dy, dz, r2, r, a, ynew As Double
        Const pp = 4              ' processor number or thread 
        sec1 = Second(Now)
        pstr = pp - 1
        If Trace = 1 Then Debug.Print("PP " + Str(pp) + " Load")
        Do
            itercnt(pp) = itercnt(pp) + 1
            Select Case state(pp)

                Case Is = Holdst
                    System.Threading.Thread.Sleep(1)

                Case Is = CancelSt
                    ' If (worker.CancellationPending = True) Or state(pp) = CancelSt Then
                    perfs(pp) = "Canceled!"
                    state(pp) = Endst
                    If Trace = 1 Then Debug.Print("PP " + Str(pp) + " Cancelst ")
                    Exit Sub

                Case Is = StartSt
                    state(pp) = ActiveSt
                    For i = pstr To nplanets Step np
                        ax(i) = 0 : ay(i) = 0 : az(i) = 0
                        For j = 0 To nplanets
                            If i <> j Then
                                dx = x(i) - x(j) : dy = y(i) - y(j) : dz = z(i) - z(j)
                                r2 = (dx * dx + dy * dy + dz * dz) : r = Sqrt(r2)
                                a = m(j) / (r2 * r)
                                ax(i) = ax(i) - a * dx
                                ay(i) = ay(i) - a * dy
                                az(i) = az(i) - a * dz
                            End If
                        Next
                        vx(i) = vx(i) + ax(i) * dt
                        vy(i) = vy(i) + ay(i) * dt
                        vz(i) = vz(i) + az(i) * dt
                    Next
                    state(pp) = StopSt

                Case Is = StartSt1
                    state(pp) = ActiveSt
                    For i = pstr To nplanets Step np
                        x(i) = x(i) + vx(i) * dt
                        ynew = y(i) + vy(i) * dt
                        If y(i) < 0 And ynew > 0 Then Rev(i) = Rev(i) + 1
                        y(i) = ynew
                        z(i) = z(i) + vz(i) * dt
                        pl_itercnt(i) = pl_itercnt(i) + 1
                    Next
                    state(pp) = StopSt

                    sec2 = Second(Now)
                    If sec2 <> sec1 Then
                        sec1 = Second(Now)
                        seccnt(pp) = seccnt(pp) + 1
                        If seccnt(pp) <= 0 Then
                            itercnt(pp) = 0
                            perfs(pp) = 0
                        Else
                            ' worker.ReportProgress(itercnt(pp) / seccnt(pp))
                            perfs(pp) = Int(itercnt(pp) / seccnt(pp))
                        End If
                    End If
            End Select
        Loop
    End Sub

    ' This event handler 4 is where the time-consuming work is done.
    Private Sub backgroundWorker4_DoWork(ByVal sender As System.Object, ByVal e As DoWorkEventArgs) Handles BackgroundWorker4.DoWork
        Dim worker As BackgroundWorker = CType(sender, BackgroundWorker)
        Dim i, j, pstr As Integer
        Dim sec1, sec2 As Integer
        Dim dx, dy, dz, r2, r, a, ynew As Double
        Const pp = 5              ' processor number or thread 
        sec1 = Second(Now)
        pstr = pp - 1
        If Trace = 1 Then Debug.Print("PP " + Str(pp) + " Load")
        Do
            itercnt(pp) = itercnt(pp) + 1
            Select Case state(pp)

                Case Is = Holdst
                    System.Threading.Thread.Sleep(1)

                Case Is = CancelSt
                    ' If (worker.CancellationPending = True) Or state(pp) = CancelSt Then
                    perfs(pp) = "Canceled!"
                    state(pp) = Endst
                    If Trace = 1 Then Debug.Print("PP " + Str(pp) + " Cancelst ")
                    Exit Sub

                Case Is = StartSt
                    state(pp) = ActiveSt
                    For i = pstr To nplanets Step np
                        ax(i) = 0 : ay(i) = 0 : az(i) = 0
                        For j = 0 To nplanets
                            If i <> j Then
                                dx = x(i) - x(j) : dy = y(i) - y(j) : dz = z(i) - z(j)
                                r2 = (dx * dx + dy * dy + dz * dz) : r = Sqrt(r2)
                                a = m(j) / (r2 * r)
                                ax(i) = ax(i) - a * dx
                                ay(i) = ay(i) - a * dy
                                az(i) = az(i) - a * dz
                            End If
                        Next
                        vx(i) = vx(i) + ax(i) * dt
                        vy(i) = vy(i) + ay(i) * dt
                        vz(i) = vz(i) + az(i) * dt
                    Next
                    state(pp) = StopSt

                Case Is = StartSt1
                    state(pp) = ActiveSt
                    For i = pstr To nplanets Step np
                        x(i) = x(i) + vx(i) * dt
                        ynew = y(i) + vy(i) * dt
                        If y(i) < 0 And ynew > 0 Then Rev(i) = Rev(i) + 1
                        y(i) = ynew
                        z(i) = z(i) + vz(i) * dt
                        pl_itercnt(i) = pl_itercnt(i) + 1
                    Next
                    state(pp) = StopSt

                    sec2 = Second(Now)
                    If sec2 <> sec1 Then
                        sec1 = Second(Now)
                        seccnt(pp) = seccnt(pp) + 1
                        If seccnt(pp) <= 0 Then
                            itercnt(pp) = 0
                            perfs(pp) = 0
                        Else
                            ' worker.ReportProgress(itercnt(pp) / seccnt(pp))
                            perfs(pp) = Int(itercnt(pp) / seccnt(pp))
                        End If
                    End If
            End Select
        Loop
    End Sub



    Private Sub Init()

        Dim i As Integer

        For i = 0 To idim
            x(i) = 0 : y(i) = 0 : z(i) = 0
            vx(i) = 0 : vy(i) = 0 : vz(i) = 0
            ax(i) = 0 : ay(i) = 0 : az(i) = 0
            pl_itercnt(i) = 0
            Rev(i) = 0     ' Revolution counter
        Next

        m(0) = 1000
        errorc = 0

        For i = 1 To nplanets
            m(i) = 0
            x(i) = 1000 + (i - 1) * 100
            If x(i) <> 0 Then vy(i) = Sqrt(m(0) / x(i))
        Next

        Printa(x, Str(pl_itercnt(0)) + " x")
        Printa(vy, Str(pl_itercnt(0)) + " vy")

    End Sub

    Private Sub Test()
        Dim a As String
        Dim i, j As Integer
        For i = 0 To nplanets - 1
            If pl_itercnt(i) <> pl_itercnt(i + 1) Then
                errorc = 1
            End If
        Next i
        If errorc = 1 Then
            a = "Test error "
            For j = 0 To nplanets
                a = a + Str(j) + " " + Str(pl_itercnt(j)) + " "
            Next j
            Debug.Print(a)
        End If

    End Sub

    Private Sub Printa(ByVal xp(), ByVal str1)
        Dim b As String
        Dim j As Integer
        b = str1
        For j = 0 To nplanets
            b = b + Str(j) + " " + Str(xp(j)) + " "
        Next j
        Debug.Print(b)

    End Sub

    Private Sub Assign(ByVal npreq)
        ' npreq = request        np = actual 
        For i = 1 To npmax
            If npreq >= i And np < i Then
                Select Case i
                    Case Is = 1
                        If Trace = 1 Then Debug.Print("Assign " + Str(i))
                        ProcTpp1.Text = 1
                    Case Is = 2
                        BackgroundWorker1.RunWorkerAsync(i)
                        If Trace = 1 Then Debug.Print("Assign " + Str(i))
                        ProcTpp2.Text = 2
                    Case Is = 3
                        BackgroundWorker2.RunWorkerAsync(i)
                        If Trace = 1 Then Debug.Print("Assign " + Str(i))
                        ProcTpp3.Text = 3
                    Case Is = 4
                        BackgroundWorker3.RunWorkerAsync(i)
                        If Trace = 1 Then Debug.Print("Assign " + Str(i))
                        ProcTpp4.Text = 4
                    Case Is = 5
                        BackgroundWorker4.RunWorkerAsync(i)
                        If Trace = 1 Then Debug.Print("Assign " + Str(i))
                        ProcTpp5.Text = 5
                End Select
            End If
            If npreq < i And np >= i Then
                Select Case i
                    Case Is = 1
                        state(i) = CancelSt
                        If Trace = 1 Then Debug.Print("Cancel " + Str(i))
                        ProcTpp1.Text = " "
                    Case Is = 2
                        state(i) = CancelSt
                        If Trace = 1 Then Debug.Print("Cancel " + Str(i))
                        ProcTpp2.Text = " "
                    Case Is = 3
                        state(i) = CancelSt
                        If Trace = 1 Then Debug.Print("Cancel " + Str(i))
                        ProcTpp3.Text = " "
                    Case Is = 4
                        state(i) = CancelSt
                        If Trace = 1 Then Debug.Print("Cancel " + Str(i))
                        ProcTpp4.Text = " "
                    Case Is = 5
                        state(i) = CancelSt
                        If Trace = 1 Then Debug.Print("Cancel " + Str(i))
                        ProcTpp5.Text = " "
                End Select
            End If
        Next
        np = npreq
    End Sub

    ' This event handler 1 updates the progress.
    Private Sub DisplayP()
        Dim pl As Integer
        Dim r As Double
        If holdreq = 0 Then
            ResultLabelpp1.Text = perfs(1)
            ResultLabelpp2.Text = perfs(2)
            ResultLabelpp3.Text = perfs(3)
            ResultLabelpp4.Text = perfs(4)
            ResultLabelpp5.Text = perfs(5)
        End If
        seccntT.Text = Str(seccnt(1))

        pl = Val(pl1T.Text)
        If pl < 0 Then pl = 0
        x1T.Text = Str(Int(x(pl)))
        y1T.Text = Str(Int(y(pl)))
        r = Sqrt(x(pl) * x(pl) + y(pl) * y(pl))
        r1T.Text = r
        rev1T.Text = Str(Rev(pl))

        pl = Val(pl2T.Text)
        If pl < 0 Then pl = 0
        x2T.Text = Str(Int(x(pl)))
        y2T.Text = Str(Int(y(pl)))
        r = Sqrt(x(pl) * x(pl) + y(pl) * y(pl))
        r2T.Text = r
        rev2T.Text = Str(Rev(pl))

        pl = Val(pl3T.Text)
        If pl < 0 Then pl = 0
        x3T.Text = Str(Int(x(pl)))
        y3T.Text = Str(Int(y(pl)))
        r = Sqrt(x(pl) * x(pl) + y(pl) * y(pl))
        r3T.Text = r
        rev3T.Text = Str(Rev(pl))

        pl = Val(pl4T.Text)
        If pl < 0 Then pl = 0
        x4T.Text = Str(Int(x(pl)))
        y4T.Text = Str(Int(y(pl)))
        r = Sqrt(x(pl) * x(pl) + y(pl) * y(pl))
        r4T.Text = r
        rev4T.Text = Str(Rev(pl))
    End Sub



    Sub InitForm()
        Dim hh, dhh, dd, ddd As Single
        '
        'Row  1
        '
        hh = 12 : dhh = 34
        Me.stateL.Location = New System.Drawing.Point(3, hh)
        Me.stateL.Size = New System.Drawing.Size(32, 13)

        dd = 60 : ddd = 70
        Me.StateTpp1.Location = New System.Drawing.Point(dd, hh)
        Me.StateTpp1.Size = New System.Drawing.Size(64, 20)

        dd = dd + ddd
        Me.StateTpp2.Location = New System.Drawing.Point(dd, hh)
        Me.StateTpp2.Size = New System.Drawing.Size(64, 20)

        dd = dd + ddd
        Me.StateTpp3.Location = New System.Drawing.Point(dd, hh)
        Me.StateTpp3.Size = New System.Drawing.Size(64, 20)

        dd = dd + ddd
        Me.StateTpp4.Location = New System.Drawing.Point(dd, hh)
        Me.StateTpp4.Size = New System.Drawing.Size(64, 20)

        dd = dd + ddd
        Me.StateTpp5.Location = New System.Drawing.Point(dd, hh)
        Me.StateTpp5.Size = New System.Drawing.Size(64, 20)
        '
        'Row  2
        '
        hh = hh + dhh
        Me.ProcL.Location = New System.Drawing.Point(3, hh)
        Me.ProcL.Size = New System.Drawing.Size(39, 13)

        dd = 60
        Me.ProcTpp1.Location = New System.Drawing.Point(dd, hh)
        Me.ProcTpp1.Size = New System.Drawing.Size(64, 20)

        dd = dd + ddd
        Me.ProcTpp2.Location = New System.Drawing.Point(dd, hh)
        Me.ProcTpp2.Size = New System.Drawing.Size(64, 20)

        dd = dd + ddd
        Me.ProcTpp3.Location = New System.Drawing.Point(dd, hh)
        Me.ProcTpp3.Size = New System.Drawing.Size(64, 20)

        dd = dd + ddd
        Me.ProcTpp4.Location = New System.Drawing.Point(dd, hh)
        Me.ProcTpp4.Size = New System.Drawing.Size(64, 20)

        dd = dd + ddd
        Me.ProcTpp5.Location = New System.Drawing.Point(dd, hh)
        Me.ProcTpp5.Size = New System.Drawing.Size(64, 20)
        '
        'Row 3
        '
        hh = hh + dhh
        Me.Label2.Location = New System.Drawing.Point(3, hh)
        Me.Label2.Size = New System.Drawing.Size(67, 13)

        dd = 85
        Me.ResultLabelpp1.Location = New System.Drawing.Point(dd, hh)
        Me.ResultLabelpp1.Size = New System.Drawing.Size(70, 13)

        dd = dd + ddd
        Me.ResultLabelpp2.Location = New System.Drawing.Point(dd, hh)
        Me.ResultLabelpp2.Size = New System.Drawing.Size(60, 13)

        dd = dd + ddd
        Me.ResultLabelpp3.Location = New System.Drawing.Point(dd, hh)
        Me.ResultLabelpp3.Size = New System.Drawing.Size(70, 13)

        dd = dd + ddd
        Me.ResultLabelpp4.Location = New System.Drawing.Point(dd, hh)
        Me.ResultLabelpp4.Size = New System.Drawing.Size(60, 13)

        dd = dd + ddd
        Me.ResultLabelpp5.Location = New System.Drawing.Point(dd, hh)
        Me.ResultLabelpp5.Size = New System.Drawing.Size(70, 13)
        '
        'Row  4
        '
        dd = 20 : ddd = 95
        hh = hh + dhh
        Me.nProcL.Location = New System.Drawing.Point(20, hh)
        Me.nProcL.Size = New System.Drawing.Size(39, 13)

        dd = dd + 39
        Me.nProcT.Location = New System.Drawing.Point(dd, hh)
        Me.nProcT.Size = New System.Drawing.Size(41, 20)

        dd = dd + 41
        Me.tdeltaL.Location = New System.Drawing.Point(dd, hh)
        Me.tdeltaL.Size = New System.Drawing.Size(39, 13)

        dd = dd + 39
        Me.tdeltaT.Location = New System.Drawing.Point(dd, hh)
        Me.tdeltaT.Size = New System.Drawing.Size(40, 20)

        dd = dd + 40
        Me.seccntL.Location = New System.Drawing.Point(dd, hh)
        Me.seccntL.Size = New System.Drawing.Size(24, 13)

        dd = dd + seccntL.Width
        Me.seccntT.Location = New System.Drawing.Point(dd, hh)
        Me.seccntT.Size = New System.Drawing.Size(40, 20)

        dd = dd + seccntT.Width
        Me.nplL.Location = New System.Drawing.Point(dd, hh)
        Me.nplL.Size = New System.Drawing.Size(51, 13)

        dd = dd + nplL.Width
        Me.nplT.Location = New System.Drawing.Point(dd, hh)
        Me.nplT.Size = New System.Drawing.Size(40, 20)

        dd = dd + nplT.Width
        Me.HoldL.Location = New System.Drawing.Point(dd, hh)
        Me.HoldL.Size = New System.Drawing.Size(29, 13)

        dd = dd + HoldL.Width
        Me.HoldT.Location = New System.Drawing.Point(dd, hh)
        Me.HoldT.Size = New System.Drawing.Size(40, 20)

        '
        ' row 5 startAsyncButton   cancelAsyncButton  endAsyncButton   MonitorButton
        '
        dd = 60
        hh = hh + dhh
        Me.StartAsyncButton.Location = New System.Drawing.Point(dd, hh)
        Me.StartAsyncButton.Size = New System.Drawing.Size(63, 25)

        dd = dd + ddd
        Me.CancelAsyncButton.Enabled = False
        Me.CancelAsyncButton.Location = New System.Drawing.Point(dd, hh)
        Me.CancelAsyncButton.Size = New System.Drawing.Size(63, 25)

        dd = dd + ddd
        Me.EndAsyncButton.Location = New System.Drawing.Point(dd, hh)
        Me.EndAsyncButton.Size = New System.Drawing.Size(63, 25)

        dd = dd + ddd
        Me.MonitorButton.Location = New System.Drawing.Point(dd, hh)
        Me.MonitorButton.Size = New System.Drawing.Size(63, 25)
        '
        'Row  6    Planet  x  y  r  rev
        '
        dd = 80
        hh = hh + dhh
        Me.Label1.Location = New System.Drawing.Point(6, hh)
        Me.Label1.Size = New System.Drawing.Size(40, 13)

        Me.Label3.Location = New System.Drawing.Point(dd, hh)
        Me.Label3.Size = New System.Drawing.Size(12, 13)

        dd = dd + ddd
        Me.Label4.Location = New System.Drawing.Point(dd, hh)
        Me.Label4.Size = New System.Drawing.Size(15, 13)

        dd = dd + ddd
        Me.Label5.Location = New System.Drawing.Point(dd, hh)
        Me.Label5.Size = New System.Drawing.Size(12, 13)

        dd = dd + ddd
        Me.Label6.Location = New System.Drawing.Point(dd, hh)
        Me.Label6.Size = New System.Drawing.Size(22, 13)
        '
        'Row  7    Planet 1
        '
        dhh = 25
        hh = hh + dhh    'hh=200
        Me.pl1T.Location = New System.Drawing.Point(13, hh)
        Me.pl1T.Size = New System.Drawing.Size(21, 20)
        dd = 60
        Me.x1T.Location = New System.Drawing.Point(dd, hh)
        Me.x1T.Size = New System.Drawing.Size(61, 20)
        dd = dd + ddd
        Me.y1T.Location = New System.Drawing.Point(dd, hh)
        Me.y1T.Size = New System.Drawing.Size(61, 20)
        dd = dd + ddd
        Me.r1T.Location = New System.Drawing.Point(dd, hh)
        Me.r1T.Size = New System.Drawing.Size(61, 20)
        dd = dd + ddd
        Me.rev1T.Location = New System.Drawing.Point(dd, hh)
        Me.rev1T.Size = New System.Drawing.Size(61, 20)
        '
        'Row  8    Planet 2
        '
        hh = hh + dhh
        Me.pl2T.Location = New System.Drawing.Point(13, hh)
        Me.pl2T.Size = New System.Drawing.Size(21, 20)
        dd = 60
        Me.x2T.Location = New System.Drawing.Point(dd, hh)
        Me.x2T.Size = New System.Drawing.Size(61, 20)
        dd = dd + ddd
        Me.y2T.Location = New System.Drawing.Point(dd, hh)
        Me.y2T.Size = New System.Drawing.Size(61, 20)
        dd = dd + ddd
        Me.r2T.Location = New System.Drawing.Point(dd, hh)
        Me.r2T.Size = New System.Drawing.Size(61, 20)
        dd = dd + ddd
        Me.rev2T.Location = New System.Drawing.Point(dd, hh)
        Me.rev2T.Size = New System.Drawing.Size(61, 20)
        '
        'Row  9    Planet 3
        '
        hh = hh + dhh
        Me.pl3T.Location = New System.Drawing.Point(13, hh)
        Me.pl3T.Size = New System.Drawing.Size(21, 20)
        dd = 60
        Me.x3T.Location = New System.Drawing.Point(dd, hh)
        Me.x3T.Size = New System.Drawing.Size(61, 20)
        dd = dd + ddd
        Me.y3T.Location = New System.Drawing.Point(dd, hh)
        Me.y3T.Size = New System.Drawing.Size(61, 20)
        dd = dd + ddd
        Me.r3T.Location = New System.Drawing.Point(dd, hh)
        Me.r3T.Size = New System.Drawing.Size(61, 20)
        dd = dd + ddd
        Me.rev3T.Location = New System.Drawing.Point(dd, hh)
        Me.rev3T.Size = New System.Drawing.Size(61, 20)
        '
        'Row  10    Planet 4
        '
        hh = hh + dhh
        Me.pl4T.Location = New System.Drawing.Point(13, hh)
        Me.pl4T.Size = New System.Drawing.Size(21, 20)
        dd = 60
        Me.x4T.Location = New System.Drawing.Point(dd, hh)
        Me.x4T.Size = New System.Drawing.Size(61, 20)
        dd = dd + ddd
        Me.y4T.Location = New System.Drawing.Point(dd, hh)
        Me.y4T.Size = New System.Drawing.Size(61, 20)
        dd = dd + ddd
        Me.r4T.Location = New System.Drawing.Point(dd, hh)
        Me.r4T.Size = New System.Drawing.Size(61, 20)
        dd = dd + ddd
        Me.rev4T.Location = New System.Drawing.Point(dd, hh)
        Me.rev4T.Size = New System.Drawing.Size(61, 20)
        '
        'PlanetForm
        '
        Me.ClientSize = New System.Drawing.Size(dd + ddd - 25, hh + dhh + 10)
        '
        'backgroundWorkerpp1
        '
        Me.BackgroundWorker1.WorkerReportsProgress = True
        Me.BackgroundWorker1.WorkerSupportsCancellation = True
        Me.BackgroundWorker2.WorkerReportsProgress = True
        Me.BackgroundWorker2.WorkerSupportsCancellation = True
        Me.BackgroundWorker3.WorkerReportsProgress = True
        Me.BackgroundWorker3.WorkerSupportsCancellation = True
        Me.BackgroundWorker4.WorkerReportsProgress = True
        Me.BackgroundWorker4.WorkerSupportsCancellation = True

    End Sub



     _
    Shared Sub Main()
        Application.Run(New PlanetPPForm)
    End Sub 'Main


End Class