- Multithreading in Visual Basic 2008
- Imports System
- Imports System.Threading
- Imports System.Runtime.InteropServices
- Public Class Read_seria1_and_Decode
- Public w As Integer = 0
- Public Delegate Function decode_1_Caller(ByVal val1 As Integer) As Double
- Public Delegate Function decode_2_Caller(ByVal val1 As Integer) As Double
- Public Event RSaD_Complete(ByVal status As String)
- Public Sub main()
- While 1
- w = w + 1
- Dim caller1 As New decode_1_Caller(AddressOf decode1)
- Dim caller2 As New decode_1_Caller(AddressOf decode1)
- Dim caller3 As New decode_2_Caller(AddressOf decode2)
- Dim tx As String = readSerial1()
- Dim result1 As IAsyncResult = caller1.BeginInvoke(1, Nothing, Nothing)
- Dim result2 As IAsyncResult = caller2.BeginInvoke(2, Nothing, Nothing)
- Dim result3 As IAsyncResult = caller3.BeginInvoke(3000, Nothing, Nothing)
- Dim returnvalue1 As Double = caller1.EndInvoke(result1)
- Dim returnvalue2 As Double = caller2.EndInvoke(result2)
- Dim returnvalue3 As Double = caller3.EndInvoke(result3)
- RaiseEvent RSaD_Complete(tx & " " & returnvalue1.ToString & " " & returnvalue2.ToString & " " & returnvalue3.ToString & "....." & w.ToString)
- End While
- End Sub
- Public Function readSerial1() As String
- Thread.Sleep(200)
- readSerial1 = "Reading Finished"
- End Function
- Public Function decode1(ByVal val1 As Integer) As Double
- Thread.Sleep(100)
- Return val1
- End Function
- Public Function decode2(ByVal val2 As Integer) As Double
- Thread.Sleep(100)
- Return 10.225 ' return something
- End Function
- End Class
- Imports System
- Imports System.Threading
- Imports System.Runtime.InteropServices
- Public Class Form1
- Private WithEvents RSad As Read_seria1_and_Decode
- Private WithEvents rs2 As Read_Serial2 'pointer to class Serila2
- Private Delegate Sub update_values_Delegate(ByVal tex As String)
- Private Delegate Sub update_values_Delegate(ByVal tex As String)
- Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
- My.Computer.Audio.Play("C:Documents and SettingstassosDesktopengine.wav", AudioPlayMode.BackgroundLoop)
- RSad = New Read_seria1_and_Decode
- Dim thread_RSad As Thread = New Thread(AddressOf RSad.main)
- thread_RSad.IsBackground = True
- thread_RSad.Start()
- Me.Timer_Read_serial2.Enabled = True
- End Sub
- Private Sub Read_Serial2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer_Read_serial2.Tick
- Me.rs2 = New Read_Serial2
- Dim thread_rs2 As Thread = New Thread(AddressOf rs2.read2)
- thread_rs2.IsBackground = True
- thread_rs2.Start("COM2")
- End Sub
- Public Sub RSaD_event_handler(ByVal status As String) Handles RSad.RSaD_Complete
- Me.update_values(status)
- End Sub
- Public Sub serial_read_2_event_handler(ByVal status As String) Handles rs2.read2_ThreadComplete
- Me.update_values(status)
- Me.rs2 = Nothing
- End Sub
- Private Sub update_values(ByVal tex As String)
- If Me.RichTextBox1.InvokeRequired Then 'See if we need to cross threads
- Me.Invoke(New update_values_Delegate(AddressOf update_values), New Object() {tex}) 'If so, have the UI thread call this method for us
- Else
- If tex <> Nothing Then
- Me.RichTextBox1.SelectedText = Environment.NewLine & tex
- End If
- End If
- End Sub
- End class