473,473 Members | 1,864 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Threads and Looping

27 New Member
I've come up with a short test program in VB.NET to see if I understand threads run in a loop, and it's obvious I don't. Please take a look at the code below and explain why I'm not getting a MsgBox back after each loop.

Imports System.Threading

Public Class Form1

Dim WithEvents oSquare As SquareClass
Dim t As Thread

Public MySquare As Double

Private Sub SquareButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SquareButton.Click

Dim x As Integer = Convert.ToInt32(TextBox1.Text)
For i As Integer = 0 To x
oSquare = New SquareClass()
oSquare.Value = Convert.ToDouble(i)

t = New Thread(AddressOf oSquare.CalcSquare)
t.Start()
Next
End Sub

Sub SquareEventHandler(ByVal Square As Double) _
Handles oSquare.ThreadComplete

MySquare = Square
MsgBox("The square is " & Square)

End Sub
End Class

Imports System.Threading

Public Class SquareClass

Public Value As Double
Public Square As Double

Public Event ThreadComplete(ByVal Square As Double)

Public Sub CalcSquare()
Thread.Sleep(1000)
SyncLock GetType(SquareClass)
Square = Value * Value
RaiseEvent ThreadComplete(Square)
End SyncLock
End Sub
End Class
Sep 20 '07 #1
14 1691
Plater
7,872 Recognized Expert Expert
Maybe I don't understand vb syntax but, you don't appear to assign your event handlers?
Sep 20 '07 #2
jwmaiden
27 New Member
I thought I did, since I have SquareEventHandler(ByVal Square As Double) _
Handles oSquare.ThreadComplete. What else should I have done?
Sep 20 '07 #3
Plater
7,872 Recognized Expert Expert
I thought that might be considered assigning.

This IS vb.net right?
Isn't the object called MessageBox.Show() in .Net? Its what I always use.

I just dumped your code into my project and it worked fine?

EDIT:
I just noticed that my converter thing added this line:
Expand|Select|Wrap|Line Numbers
  1. AddHandler oSquare.ThreadComplete, AddressOf SquareEventHandler
  2.  
inside your loop
Sep 20 '07 #4
jwmaiden
27 New Member
Yes, this is vb.net. A MsgBox is a MessageBox is a MsgBox.

If it works you should have had multiple windows that open, each giving you the squared value. For example, use 4 as the initial value. You should have to close the Message Box five times. When I do it, I only get one window, which means that the event handler is not being called enough.
Sep 20 '07 #5
jwmaiden
27 New Member
Yes, adding the AddHandler does make it work. Thanks. What is this converter thingy you're talking about?
Sep 20 '07 #6
jwmaiden
27 New Member
This makes it look better, but I think there are still problems. For example, I get more Message Boxes than I should. If I input 3, I should get 4 boxes, but I get 5 (the 5th one just repeats the last value). I've also got some random results (numbers returned out of order). I think that threads are not operating in order, and this is producing weird behavior.
Sep 20 '07 #7
Plater
7,872 Recognized Expert Expert
They're out of order because of the nature of threads and race conditions.
You're not garunteed who will finish "first" in this scenario.

As for why you get an extra messagebox that I don't know.


And the converter I used is here:
http://labs.developerfusion.co.uk/co...arp-to-vb.aspx
Sep 20 '07 #8
jwmaiden
27 New Member
Is there a way for me to guarantee that threads will operate in order?

My main issue is that I want to be able to perform actions after the thread returns from performing the required task. In other words, if I needed to use the value MySquare and do something with it, I want to guarantee that the thread actually returned Square, waits for me to finish doing whatever I want to MySquare, and then gets the next variable in the loop.
Sep 20 '07 #9
Plater
7,872 Recognized Expert Expert
Is there a way for me to guarantee that threads will operate in order?
Threads are not what you're looking for here then.
If you want to wait until a thread is done before moving on AND you want it to go in order, then you just want a loop, no need for threads.
Sep 20 '07 #10
jwmaiden
27 New Member
That's what I originally thought. I wrote this sample because I have a much more complicated program that uses a similar principle. In the other program, I call in a line of data from an Excel file, pass the data to a dll, and get back a number. There are multiple lines of data that I'm calling in from Excel, so all of this was being run in a loop. I was getting issues with CoWaitForMultipleHandles, and I thought that putting everything in threads would solve that problem.
Sep 20 '07 #11
Plater
7,872 Recognized Expert Expert
You could put the loop ITSELF inside a serperate thread from your other program (is nice if you have a gui that needs to keep updating what it displays)

If you don't care the order, then putting them in threads is ok because you can just handle whatever one finishes and be done with it.
Sep 20 '07 #12
jwmaiden
27 New Member
Good idea. I wouldn't expect anything less from an RPI grad.
Sep 20 '07 #13
Plater
7,872 Recognized Expert Expert
Good idea. I wouldn't expect anything less from an RPI grad.
Haha someone's actually heard of that crummy school?
Sep 21 '07 #14
jwmaiden
27 New Member
I spent a summer there doing summer research. Trust me, there are worse places to be in NY than Troy.
Sep 21 '07 #15

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

Similar topics

9
by: Nicolas Fleury | last post by:
Hi everyone, I just discovered, correct me if I'm wrong, that as long as a threading.Thread is running, its destructor will not be called, because it is referring to himself. So if I have...
2
by: Laszlo Szijarto | last post by:
I was very excited to find out that .NET supported multi-threading. I have long had issues with Forms freezing up while large datasets (recordsets) were being collected or with various looping...
2
by: Adam Clauss | last post by:
OK, my main thread will create some number of other threads (number determined at runtime). Each of these threads creates a form via a call: private void ProcessThread() { Application.Run(new...
10
by: HK | last post by:
With VB.NET 2005, and a Windows Form, running on a dual CPU box, I need to take a recordset (e.g. 100,000 records) and spawn a thread to handle an internet XML transaction routine for each of the...
4
by: Gregory Gadow | last post by:
I've cobbled together a PrinterClass that takes a text file and dumps it to a printer. The app using is has multiple threads, all of which need access to a shared instance. Can someone point me to...
2
by: Marcus Kwok | last post by:
I have processing code (I'll call it the "model") written in native unmanaged pure C++, and I have put a GUI on top of it written using Windows Forms (.NET 1.1). The GUI is used to set the...
1
by: Peter | last post by:
Hi, I have a Dictionary<key, valuewhich is accessed by three threads. One thread puts my value objects in the dictionary (occasionally), and also updates the contents of existing value objects -...
4
by: tdahsu | last post by:
All, I'd appreciate any help. I've got a list of files in a directory, and I'd like to iterate through that list and process each one. Rather than do that serially, I was thinking I should...
12
by: Curious | last post by:
I'm working on a multi-threading project and I've got an issue about shared memory used by multiple threads at the same time. I have two separate threads that have different callback methods....
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.