Search This Blog

Tuesday, March 9, 2010

Threading Example in vb.net

Imports System.Threading

Public Class setting_form   'Form
    Dim valid As Boolean = False
    'CREATE A THREAD AND DELEGATE
    Public Thread1 As Thread
    Private Delegate Sub updateParameters()
    Dim del As updateParameters
    Dim Result As IAsyncResult

    Private Sub read_write_all_signals()      
        'DELEGATE FUNCTION CALLING ALL THE READING WRITING FUNCTIONS
        Label2.Text = tot.ToString
       'ur code here
    End Sub

    Private Sub button_test_digital_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        valid = Validate_data()
        If valid = True Then
            read_digital_inputs()
            write_digital_outputs()
        End If
    End Sub

    Private Sub button_test_analog_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        valid = Validate_data()
        If valid = True Then
            read_analog_inputs()
            write_analog_outputs()
        End If
    End Sub

    Private Sub button_test_both_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button_test_both.Click
        'BUTTON TO START OR RESUME THE THREAD
        If (Thread1.ThreadState And ThreadState.Unstarted) <> 0 Then
            'The thread has never been started. Start it.
            Thread1.Start()
            'MessageBox.Show("Thread started")
        Else
            ' The thread is paused. Resume it.
            Thread1.Resume()
            'MessageBox.Show("Thread restarted")
        End If

        button_reset_both.Enabled = True
        button_test_both.Enabled = False
    End Sub
    Private Sub button_reset_both_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button_reset_both.Click
        'SUSPEND THE THREAD
        If (Thread1.ThreadState And ThreadState.Unstarted) <> 1 Then
            'The thread has never been started. Start it.
            Thread1.Suspend()
            'MessageBox.Show("Thread Suspended.")
        End If
        button_reset_both.Enabled = False
        button_test_both.Enabled = True
    End Sub

    Private Sub setting_form_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'INITIALIZE THE THREAD ON LOADING CURRENT FORM
        Thread1 = New Thread(AddressOf Count)
        Thread1.IsBackground = True
    End Sub
    Public Sub Count()
        'THIS IS THE THREAD CODE
        Try
            Do
                'Wait 1 second.
                Thread.Sleep(1000)
                SyncLock Me
                    tot = tot + 1
                    'DelegateWork function calls all the READING WRITING functions
                    del = New updateParameters(AddressOf read_write_all_signals)
                    Result = BeginInvoke(del)
                End SyncLock
            Loop
        Catch ex As ThreadAbortException
            'This happens when the main program aborts the
            'thread.
        End Try
    End Sub

    Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
       exit_me()
    End Sub
    Public Sub exit_me()
        If MessageBox.Show("Are you Sure", "Exit Tester", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) = Windows.Forms.DialogResult.OK Then
            If (Thread1.ThreadState And ThreadState.Unstarted) <> 0 Then
                'The thread has never been started. Start it.
                Thread1.Abort()
            End If
            Me.Close()
            Start.Close()
        End If
    End Sub
End Class

No comments:

Post a Comment