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
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?
I thought I did, since I have SquareEventHandler(ByVal Square As Double) _
Handles oSquare.ThreadComplete. What else should I have done?
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: -
AddHandler oSquare.ThreadComplete, AddressOf SquareEventHandler
-
inside your loop
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.
Yes, adding the AddHandler does make it work. Thanks. What is this converter thingy you're talking about?
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.
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
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.
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.
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.
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.
Good idea. I wouldn't expect anything less from an RPI grad.
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?
I spent a summer there doing summer research. Trust me, there are worse places to be in NY than Troy.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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...
|
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...
|
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...
|
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...
| |
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...
|
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 -...
|
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...
|
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....
|
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,...
|
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,...
| |
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...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |