All pastes #551389 Raw Edit

Untitled

public text v1 · immutable
·published 2007-06-08 19:48 UTC
rendered paste body
Imports System.IO.Ports
Imports System.IO
Imports System.Threading.Thread
Imports System.Threading



Public Class Form1

    Dim sp As New SerialPort
    Dim t As New System.Threading.Thread(AddressOf calc)
    Dim sw As New Stopwatch
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        asdf()
    End Sub
    Sub calc()
        sw.Start()
        Do Until sw.ElapsedMilliseconds = 50000
            System.Threading.Thread.Sleep(200)
        Loop
    End Sub
    Sub asdf()
        Dim count As Integer = 0
        Me.Refresh()
        If CBool(Threading.ThreadState.Running) = True Then
            t.Start()
        End If

        Do Until count = 1000000
            updatelbl()
            System.Threading.Thread.Sleep(100)
            count = count + 1
        Loop
    End Sub
    Sub updatelbl()
        Label1.Text = sw.ElapsedMilliseconds.ToString
        Label1.Refresh()
    End Sub
    Public Sub New()
        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        sp.Encoding = System.Text.Encoding.Default
        sp.Open()
        GetSerialPortNames()
        t.Start()
    End Sub
    Private Sub GetSerialPortNames()
        ' Show all available COM ports.
        For Each pn As String In My.Computer.Ports.SerialPortNames
            cbxportname.Items.Add(pn)
        Next
        'load the current port into the combobox
        cbxportname.SelectedItem = sp.PortName.ToString
    End Sub

    Private Sub cbxportname_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbxportname.SelectedIndexChanged
        Try  ' updates the com port with the user's selection
            sp.Close()
            sp.PortName = cbxportname.SelectedItem.ToString
            sp.BaudRate = 19200
            sp.Open()
        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try
    End Sub

    Private Sub Form1_Disposed(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Disposed
        sp.Close()
        Me.Dispose()
    End Sub

    Private Sub Form1_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        sp.Close()
        Me.Dispose()
    End Sub

    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        sp.Close()
        Me.Dispose()
    End Sub
End Class