473,397 Members | 1,949 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

multithreading HELP

OK... here goes. I'm not new to VB.net, but I AM new to multi-threading.
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
  1. Imports System
  2. Public Class frmMain
  3.     Dim backThread1, backThread2, backThread3 As Threading.Thread
  4.     Dim hexString1, hexString2, hexString3, finalString As String
  5.     Dim FileRead As IO.StreamReader
  6.     Dim FileWrite As IO.StreamWriter
  7.     Dim FileName As String
  8.     Dim hexCount
  9.     Private Sub btnGenerate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGenerate.Click
  10.         btnGenerate.Enabled = False
  11.         backThread1 = New Threading.Thread(AddressOf Me.BackgroundWorks1)
  12.         backThread2 = New Threading.Thread(AddressOf Me.BackgroundWorks2)
  13.         backThread3 = New Threading.Thread(AddressOf Me.BackgroundWorks3)
  14.         backThread1.Start()
  15.     End Sub
  16.  
  17.     Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
  18.         If backThread1.IsAlive Then
  19.             backThread1.Abort()
  20.         End If
  21.         Me.Close()
  22.     End Sub
  23.  
  24.     Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  25.         FileName = "C:\16-mil-hexcol.dat"
  26.         ProgressBar1.Minimum = 0
  27.         ProgressBar1.Maximum = 255
  28.         ProgressBar1.Value = 0
  29.         ProgressBar2.Minimum = 0
  30.         ProgressBar2.Maximum = 255
  31.         ProgressBar2.Value = 0
  32.         ProgressBar3.Minimum = 0
  33.         ProgressBar3.Maximum = 255
  34.         ProgressBar3.Value = 0
  35.     End Sub
  36.  
  37.     Private Sub BackgroundWorks1()
  38.         Dim count1 As Integer = 0
  39.         For count1 = 0 To 255
  40.             hexCount = Hex(count1)
  41.             hexString1 = Convert.ToString(hexCount)
  42.             If count1 < 255 Then
  43.                 If Me.TextBox1.InvokeRequired Then
  44.                     ' It's on a different thread, so use Invoke.
  45.                     Dim d As New CallType
  46.                     Me.Invoke(d, New Object() {[NewText] + " (Invoke)"})
  47.                 Else
  48.                     ' It's on the same thread, no need for Invoke.
  49.                     Me.TextBox1.Text = [NewText] + " (No Invoke)"
  50.                 End If
  51.  
  52.                 ProgressBar1.Value += 1
  53.             ElseIf count1 = 255 Then
  54.                 ProgressBar1.Value = 0
  55.             End If
  56.             Call BackgroundWorks2()
  57.             FileWrite = IO.File.AppendText(FileName)
  58.             FileWrite.Write(finalString)
  59.             FileWrite.Close()
  60.             finalString = ""
  61.             hexString1 = ""
  62.             hexString2 = ""
  63.             hexString3 = ""
  64.         Next count1
  65.         FileRead = IO.File.OpenText(FileName)
  66.         TextBox1.Text = FileRead.ReadToEnd()
  67.         FileRead.Close()
  68.     End Sub
  69.  
  70.     Private Sub BackgroundWorks2()
  71.         Dim count2 As Integer = 0
  72.         backThread2.Start()
  73.         For count2 = 0 To 255
  74.             hexCount = Hex(count2)
  75.             hexString2 = Convert.ToString(hexCount)
  76.             If count2 < 255 Then
  77.                 ProgressBar2.Value += 1
  78.             ElseIf count2 = 255 Then
  79.                 ProgressBar2.Value = 0
  80.             End If
  81.             Call BackgroundWorks3()
  82.         Next count2
  83.     End Sub
  84.  
  85.     Private Sub BackgroundWorks3()
  86.         Dim count3 As Integer = 0
  87.         backThread3.Start()
  88.         For count3 = 0 To 255
  89.             hexCount = Hex(count3)
  90.             hexString3 = Convert.ToString(hexCount)
  91.             If hexString3.Length() <= 1 Then
  92.                 hexString3 = CStr("0" & hexString3)
  93.             End If
  94.             If hexString2.Length() <= 1 Then
  95.                 hexString2 = CStr("0" & hexString2)
  96.             End If
  97.             If hexString1.Length() <= 1 Then
  98.                 hexString1 = CStr("0" & hexString1)
  99.             End If
  100.             finalString &= CStr(hexString3 & " " & hexString2 & " " & hexString1 & " 00 ")
  101.             If count3 < 255 Then
  102.                 ProgressBar3.Value += 1
  103.             ElseIf count3 = 255 Then
  104.                 ProgressBar3.Value = 0
  105.             End If
  106.         Next count3
  107.     End Sub
  108. End Class
  109.  
Jan 18 '09 #1
0 1098

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: Mark Yudkin | last post by:
The documentation is unclear (at least to me) on the permissibility of accessing DB2 (8.1.5) concurrently on and from Windows 2000 / XP / 2003, with separate transactions scope, from separate...
16
by: Robert Zurer | last post by:
Can anyone suggest the best book or part of a book on this subject. I'm looking for an in-depth treatment with examples in C# TIA Robert Zurer robert@zurer.com
9
by: Popoxinhxan | last post by:
Dear experts, i want to develop an client application that consume the google search web service. In my MainForm i have a method to retrieve all the search result e.g. GetGoogleResults(). Now i...
5
by: sarge | last post by:
I would like to know how to perform simple multithreading. I had created a simple form to test out if I was multithreading properly, but got buggy results. Sometime the whole thig would lock up...
2
by: Rich | last post by:
Hello, I have set up a multithreading routine in a Test VB.net proj, and it appears to be working OK in debug mode and I am not using synchronization. Multithreading is a new thing for me, and...
55
by: Sam | last post by:
Hi, I have a serious issue using multithreading. A sample application showing my issue can be downloaded here: http://graphicsxp.free.fr/WindowsApplication11.zip The problem is that I need to...
5
by: sandy82 | last post by:
Whats actuallly multithreading is ... and how threading and multithreading differ . Can any1 guide how multithreading is used on the Web .. i mean a practical scenario in which u use...
2
by: Pradnya Patil | last post by:
hi , I am trying to draw ' html div-tag ' on the screen which will resemble a rectangle through vb.net code. I want it to be drawn faster...so I introduced multithreading using Threadpool. I...
7
by: Ray | last post by:
Hello, Greetings! I'm looking for a solid C++ multithreading book. Can you recommend one? I don't think I've seen a multithreading C++ book that everybody thinks is good (like Effective C++ or...
7
darlene
by: darlene | last post by:
Hi, I need some help in creating an application in Visual C++ which should make use of MFC and multithreading. The application is supposed to consist in a number of threads representing factories....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.