' 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 time As Label
    Private start As Button
    Private abort As Button
    Private t As Thread

    Public Sub New()
        time = New Label()
        start = New Button()
        abort = New Button()
        SuspendLayout()
        time.Location = New Point(50, 50)
        time.Size = New Size(300, 50)
        time.Font = New Font(FontFamily.GenericSerif, 16F)
        start.Location = New Point(50, 150)
        start.Size = New Size(200, 50)
        start.Name = "Start Button"
        start.Text = "Start clock"
        AddHandler start.Click, AddressOf StartClick
        abort.Location = New Point(50, 250)
        abort.Size = New Size(200, 50)
        abort.Name = "Abort Button"
        abort.Text = "Abort clock"
        AddHandler abort.Click, AddressOf AbortClick
        ClientSize = New Size(400, 350)
        Controls.Add(time)
        Controls.Add(start)
        Controls.Add(abort)
        Name = "Main Form"
        Text = "Main Form"
        ResumeLayout(False)
    End Sub
    Public Sub Run()
        While True
            If time.InvokeRequired Then
                time.Invoke(DirectCast(Sub() time.Text = DateTime.Now.ToString(), Action))
            Else
                Update()
            End If
            Thread.Sleep(1000)
        End While
    End Sub
    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
