Bust all you want about searching, but I hate reading hundreds of threads trying to find an answer to my problem when I don't really know what I'm searching for.
I need to pass information from a thread to my main thread. IE a progress bar value goes up as the thread makes progress..
I realize my code isn't the best, but I'll clean it up when I get it working faster.
By the way, there is a file write with append text. It's there as I was working on getting this program to move faster. I then discovered multi-threading and got stuck.
All critics on my code are welcome. I want to get better!
Expand|Select|Wrap|Line Numbers
- Imports System
- Public Class frmMain
- Dim backThread1, backThread2, backThread3 As Threading.Thread
- Dim hexString1, hexString2, hexString3, finalString As String
- Dim FileRead As IO.StreamReader
- Dim FileWrite As IO.StreamWriter
- Dim FileName As String
- Dim hexCount
- Private Sub btnGenerate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGenerate.Click
- btnGenerate.Enabled = False
- backThread1 = New Threading.Thread(AddressOf Me.BackgroundWorks1)
- backThread2 = New Threading.Thread(AddressOf Me.BackgroundWorks2)
- backThread3 = New Threading.Thread(AddressOf Me.BackgroundWorks3)
- backThread1.Start()
- End Sub
- Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
- If backThread1.IsAlive Then
- backThread1.Abort()
- End If
- Me.Close()
- End Sub
- Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
- FileName = "C:\16-mil-hexcol.dat"
- ProgressBar1.Minimum = 0
- ProgressBar1.Maximum = 255
- ProgressBar1.Value = 0
- ProgressBar2.Minimum = 0
- ProgressBar2.Maximum = 255
- ProgressBar2.Value = 0
- ProgressBar3.Minimum = 0
- ProgressBar3.Maximum = 255
- ProgressBar3.Value = 0
- End Sub
- Private Sub BackgroundWorks1()
- Dim count1 As Integer = 0
- For count1 = 0 To 255
- hexCount = Hex(count1)
- hexString1 = Convert.ToString(hexCount)
- If count1 < 255 Then
- If Me.TextBox1.InvokeRequired Then
- ' It's on a different thread, so use Invoke.
- Dim d As New CallType
- Me.Invoke(d, New Object() {[NewText] + " (Invoke)"})
- Else
- ' It's on the same thread, no need for Invoke.
- Me.TextBox1.Text = [NewText] + " (No Invoke)"
- End If
- ProgressBar1.Value += 1
- ElseIf count1 = 255 Then
- ProgressBar1.Value = 0
- End If
- Call BackgroundWorks2()
- FileWrite = IO.File.AppendText(FileName)
- FileWrite.Write(finalString)
- FileWrite.Close()
- finalString = ""
- hexString1 = ""
- hexString2 = ""
- hexString3 = ""
- Next count1
- FileRead = IO.File.OpenText(FileName)
- TextBox1.Text = FileRead.ReadToEnd()
- FileRead.Close()
- End Sub
- Private Sub BackgroundWorks2()
- Dim count2 As Integer = 0
- backThread2.Start()
- For count2 = 0 To 255
- hexCount = Hex(count2)
- hexString2 = Convert.ToString(hexCount)
- If count2 < 255 Then
- ProgressBar2.Value += 1
- ElseIf count2 = 255 Then
- ProgressBar2.Value = 0
- End If
- Call BackgroundWorks3()
- Next count2
- End Sub
- Private Sub BackgroundWorks3()
- Dim count3 As Integer = 0
- backThread3.Start()
- For count3 = 0 To 255
- hexCount = Hex(count3)
- hexString3 = Convert.ToString(hexCount)
- If hexString3.Length() <= 1 Then
- hexString3 = CStr("0" & hexString3)
- End If
- If hexString2.Length() <= 1 Then
- hexString2 = CStr("0" & hexString2)
- End If
- If hexString1.Length() <= 1 Then
- hexString1 = CStr("0" & hexString1)
- End If
- finalString &= CStr(hexString3 & " " & hexString2 & " " & hexString1 & " 00 ")
- If count3 < 255 Then
- ProgressBar3.Value += 1
- ElseIf count3 = 255 Then
- ProgressBar3.Value = 0
- End If
- Next count3
- End Sub
- End Class