Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 7th, 2012  |  syntax: None  |  size: 3.62 KB  |  hits: 8  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Multithreading in Visual Basic 2008
  2. Imports System
  3.         Imports System.Threading
  4.         Imports System.Runtime.InteropServices
  5.  
  6.         Public Class Read_seria1_and_Decode
  7.  
  8.     Public w As Integer = 0
  9.     Public Delegate Function decode_1_Caller(ByVal val1 As Integer) As Double
  10.     Public Delegate Function decode_2_Caller(ByVal val1 As Integer) As Double
  11.  
  12.     Public Event RSaD_Complete(ByVal status As String)
  13.  
  14.     Public Sub main()
  15.         While 1
  16.             w = w + 1
  17.             Dim caller1 As New decode_1_Caller(AddressOf decode1)
  18.             Dim caller2 As New decode_1_Caller(AddressOf decode1)
  19.             Dim caller3 As New decode_2_Caller(AddressOf decode2)
  20.  
  21.             Dim tx As String = readSerial1()
  22.  
  23.             Dim result1 As IAsyncResult = caller1.BeginInvoke(1, Nothing, Nothing)
  24.             Dim result2 As IAsyncResult = caller2.BeginInvoke(2, Nothing, Nothing)
  25.             Dim result3 As IAsyncResult = caller3.BeginInvoke(3000, Nothing, Nothing)
  26.  
  27.             Dim returnvalue1 As Double = caller1.EndInvoke(result1)
  28.             Dim returnvalue2 As Double = caller2.EndInvoke(result2)
  29.             Dim returnvalue3 As Double = caller3.EndInvoke(result3)
  30.  
  31.             RaiseEvent RSaD_Complete(tx & "    " & returnvalue1.ToString & "   " & returnvalue2.ToString & "   " & returnvalue3.ToString & "....." & w.ToString)
  32.         End While
  33.  
  34.     End Sub
  35.  
  36.     Public Function readSerial1() As String
  37.  
  38.         Thread.Sleep(200)
  39.  
  40.         readSerial1 = "Reading Finished"
  41.  
  42.     End Function
  43.  
  44.  
  45.     Public Function decode1(ByVal val1 As Integer) As Double
  46.  
  47.         Thread.Sleep(100)
  48.         Return val1
  49.  
  50.     End Function
  51.  
  52.     Public Function decode2(ByVal val2 As Integer) As Double
  53.  
  54.         Thread.Sleep(100)
  55.         Return 10.225 ' return something
  56.  
  57.     End Function
  58.  
  59. End Class
  60.        
  61. Imports System
  62.     Imports System.Threading
  63.     Imports System.Runtime.InteropServices
  64.  
  65. Public Class Form1
  66.  
  67. Private WithEvents RSad As Read_seria1_and_Decode
  68. Private WithEvents rs2 As Read_Serial2 'pointer to class Serila2
  69. Private Delegate Sub update_values_Delegate(ByVal tex As String)
  70. Private Delegate Sub update_values_Delegate(ByVal tex As String)
  71.  
  72. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  73.  
  74.         My.Computer.Audio.Play("C:Documents and SettingstassosDesktopengine.wav", AudioPlayMode.BackgroundLoop)
  75.         RSad = New Read_seria1_and_Decode
  76.  
  77.         Dim thread_RSad As Thread = New Thread(AddressOf RSad.main)
  78.         thread_RSad.IsBackground = True
  79.         thread_RSad.Start()
  80.  
  81.         Me.Timer_Read_serial2.Enabled = True
  82.  
  83.     End Sub
  84.  
  85. Private Sub Read_Serial2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer_Read_serial2.Tick
  86.  
  87.  
  88.     Me.rs2 = New Read_Serial2
  89.     Dim thread_rs2 As Thread = New Thread(AddressOf rs2.read2)
  90.     thread_rs2.IsBackground = True
  91.     thread_rs2.Start("COM2")
  92.  
  93.     End Sub
  94.  
  95. Public Sub RSaD_event_handler(ByVal status As String) Handles RSad.RSaD_Complete
  96.  
  97.     Me.update_values(status)
  98. End Sub
  99.  
  100. Public Sub serial_read_2_event_handler(ByVal status As String) Handles rs2.read2_ThreadComplete
  101.  
  102.     Me.update_values(status)
  103.  
  104.     Me.rs2 = Nothing
  105.  
  106. End Sub
  107.  
  108.  
  109.  
  110.  
  111.  Private Sub update_values(ByVal tex As String)
  112.  
  113.         If Me.RichTextBox1.InvokeRequired Then                                                           'See if we need to cross threads
  114.             Me.Invoke(New update_values_Delegate(AddressOf update_values), New Object() {tex})    'If so, have the UI thread call this method for us
  115.         Else
  116.             If tex <> Nothing Then
  117.  
  118.                 Me.RichTextBox1.SelectedText = Environment.NewLine & tex
  119.             End If
  120.         End If
  121.  
  122.     End Sub
  123.  
  124.     End class