' Collection of code snippets by Arne Vajhøj 
' (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
Imports System
Imports System.Drawing
Imports System.Windows.Forms
Imports System.Threading

Public Class MainForm
    Inherits Form
    Private math As Label
    Private start As Button
    Private abort As Button
    Private t As Thread

    Public Sub New()
        math = New Label()
        start = New Button()
        abort = New Button()
        SuspendLayout()
        math.Location = New Point(50, 50)
        math.Size = New Size(300, 50)
        math.Font = New Font(FontFamily.GenericSerif, 16F)
        start.Location = New Point(50, 150)
        start.Size = New Size(200, 50)
        start.Text = "Start calculations"
        AddHandler start.Click, AddressOf StartClick
        abort.Location = New Point(50, 250)
        abort.Size = New Size(200, 50)
        abort.Text = "Abort calculations"
        AddHandler abort.Click, AddressOf AbortClick
        ClientSize = New Size(400, 350)
        Controls.Add(math)
        Controls.Add(start)
        Controls.Add(abort)
        Name = "Main Form"
        Text = "Main Form"
        ResumeLayout(False)
    End Sub
    Public Sub Run()
        While True
            If math.InvokeRequired Then
                math.Invoke(DirectCast(Sub() math.Text = NewCalc(), Action))
            Else
                math.Text = NewCalc()
            End If
            Thread.Sleep(2000)
        End While
    End Sub
    Private Shared rng As Random = New Random()
    Private Function NewCalc() As String
        Dim v1 As Integer = rng.Next(1000)
        Dim v2 As Integer = rng.Next(1000)
        NewCalc = String.Format("{0} + {1} = {2}", v1, v2, v1+v2)
    End Function
    Public Sub StartClick(sender As Object, e As EventArgs)
        t = New Thread(AddressOf Run)
        t.Start()
    End Sub
    Public Sub AbortClick(sender As Object, e As EventArgs)
        t.Abort()
    End Sub
    <STAThread> _
    Public Shared Sub Main(args As String())
        Application.Run(New MainForm())
        Application.Exit()
        Environment.Exit(0)
    End Sub
End Class
