|
-
Jul 9th, 2002, 12:02 PM
#1
Thread Starter
Hyperactive Member
Newbie Qestion - Multithreading
I'm currently playing with multithreading in vb.net and I can run one thread with no problem. If I try to run two threads, it seems to run slowly for some strange reason. I don't know how to expain it. Maybe the code i'm using will help(wo/ form code):
Code:
Public Class Form1
Inherits System.Windows.Forms.Form
Dim t1 As System.Threading.Thread
Dim t2 As System.Threading.Thread
#Region " Windows Form Designer generated code "
Private Sub BackgroundProcess1()
'//setting up the first subroutine that will run in thread
Dim i As Integer = 1
Do While True
TextBox1.Text = "Iterations: " & i
i += 1
Loop
End Sub
Private Sub BackgroundProcess2()
'//setting up the second subroutine that will run in thread
Dim i2 As Integer = 1
Do While True
TextBox2.Text = "Iterations: " & i2
i2 += 1
Loop
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'//assigning subroutine to the thread and starting thread
t1 = New System.Threading.Thread(AddressOf Me.BackgroundProcess1)
t2 = New System.Threading.Thread(AddressOf Me.BackgroundProcess2)
t1.Start()
t2.Start()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'//setting up button that will stop and start thread
If Button1.Text = "Stop" Then
t1.Suspend()
t2.Suspend()
Button1.Text = "Start"
ElseIf Button1.Text = "Start" Then
t1.Resume()
t2.Resume()
Button1.Text = "Stop"
End If
End Sub
End Class
-
Jul 9th, 2002, 12:27 PM
#2
Both threads are sharing the same processor, that means each thread will operate slower than just one thread would.
Also, when you have more than one thread, the processor also has to spend a few extra clock cycles to switch which thread it is working on.
Basically, it comes down to this: A processor can only do so much work at once. When you start adding more work to it (threads) it will slow down some, but give the appearance of doing two things at once.
-
Jul 9th, 2002, 04:05 PM
#3
Thread Starter
Hyperactive Member
What's the point of using multithreading if you can only use one thread? I've got like 354 threads running on my computer right now. There's has to be some benefit to using multithreading.
-
Jul 9th, 2002, 04:21 PM
#4
Multithreading allows your program to do some intensive background work, and still keep the interface accessable to the user.
In VB6, if you ran a loop for a long time and did a lot of processing in that loop, the user had to wait till it was done before they could interact with the user interface because it froze...waiting for that loop to stop or for the OS to step in and yield the processor over to another process for a couple seconds. If you multithread, the the intensive loop could run on it's own thread, there by leaving your interface on it's own thread. This means that the user interface can still take the users inputs and such without them having to wait for something to get done processing.
It doesn't get things done faster, it is just percieved that way. If you have a dual processor machine, you will probably notice an increase though, because then two threads can execute at the same time, instead of yielding to the other thread every now and then.
-
Jul 9th, 2002, 04:47 PM
#5
Thread Starter
Hyperactive Member
That makes since. I wasn't expecting a speed increase, just wanted to learn about multithreading. It's still an interesting concept.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|