|
-
Jul 15th, 2003, 10:06 PM
#1
Thread Starter
Hyperactive Member
Multithreaded program
I know Intel has come out with processors that can execute two or more commands at the same time. I also know modern day operating systems Multitask. I am yet to see a compiler that could execute two functions at once. Like if there was a function that split apart a string, could you execute it at the same time simultaneously. Then again it may just be a dumb idea unless you can access the processor, because if not, your program would execute slower. Oh well just an idea.
-
Jul 15th, 2003, 10:26 PM
#2
Any processor can multithread, go to the performance tab in the Task Manager and it will say how many threads are currently running (mine is 572 right now). So whats the question?
-
Jul 15th, 2003, 10:51 PM
#3
Thread Starter
Hyperactive Member
My question is. Can vb execute more then one line of code at a time.
-
Jul 15th, 2003, 11:20 PM
#4
yay gay
lol yes it can. use the System.Threading namespace...and the class Thread
\m/  \m/
-
Jul 16th, 2003, 12:10 AM
#5
Thread Starter
Hyperactive Member
Ok, how and the heck do i do that? I guess say i am calling a function sort that receives a string as it's only paramiter. just an example and that's about all ill need.
-
Jul 16th, 2003, 12:51 AM
#6
When using the Thread class you can't pass parameters directly to the method so you can do any number of workarounds for this, usually you set the parameters via some form of persistable object or variables then have the no parameter method that you start with the threading class check on them.
You can also use a delegate and the BeginInvoke method to execute a method and pass in parameters. This requires that you have a delegate setup and that you know the method signature.
One think to remember is that no two threads can access the same resource at the sametime, or at least you must be very careful about this. Also controls and things with a visual aspect must be created in the thread that carries the UI thread or the application main thread.
Here is an example for the delegate approach:
http://www.edneeis.com/Code/BeginInvoke.asp
-
Jul 16th, 2003, 08:59 AM
#7
Frenzied Member
Threading has nothing to do with multiple instructions executing
at the SAME time. Even if you have a single thread process, it
will run faster on a multi-instruction processor or on multiple
processors because instructions will execute at EXACTLY the same
time. All processes regardless of whether or not they're multi-
threaded will execute instructions simultaneously.
Threading just interleaves the instructions of one branch of
code with another, meaning that the processor alternates
executing instructions of different threads. Let's say you have a
an interface and a lengthy loop but you want the interface to
update while the loop is running. Unless you create a thread to
execute the loop, no other code will run and the interface won't
update until the loop is done.
I hope that clears it up.
Last edited by wey97; Jul 17th, 2003 at 06:05 AM.
-
Jul 16th, 2003, 12:12 PM
#8
Sleep mode
Would this make any sense ? .
VB Code:
Public Sub thread1()
MessageBox.Show("Thread 1 fired")
End Sub
Public Sub thread2()
MessageBox.Show("Thread 2 fired")
End Sub
Public Sub AllThread()
'Combine the two subs in one sub so they can be
'fired at once
Me.thread1()
Me.thread2()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles Button1.Click
Dim thrd As Threading.Thread
thrd = New Threading.Thread(AddressOf AllThread)
thrd.Start()
End Sub
-
Jul 16th, 2003, 08:36 PM
#9
Thread Starter
Hyperactive Member
Pirate, your the man. That was along the lines of exactly what i needed to do, except thread1 and thread2 are a bit more complex...thank you so much for every one's help.
-
Jul 16th, 2003, 09:30 PM
#10
Here is an example using delegates:
VB Code:
Public Delegate Function MessageBoxShowHandler(ByVal test As String) As DialogResult
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim del As New MessageBoxShowHandler(AddressOf MessageBox.Show)
del.BeginInvoke("Thread 1 fired", Nothing, Nothing)
del.BeginInvoke("Thread 2 fired", Nothing, Nothing)
MessageBox.Show("Notice that the other messageboxes didn't stop execution of this thread since they are on their own thread.")
MessageBox.Show("I got held up because the last messagebox wasn't on a different thread.")
End Sub
Last edited by Edneeis; Jul 16th, 2003 at 09:34 PM.
-
Jul 16th, 2003, 09:42 PM
#11
Frenzied Member
The code that Pirate showed wont really solve the problem. Even though you are spinning up a second thread, thread2 wont execute until thread1 is finish. So if thread1 is a long running method, thread2 wont be executed until it is finished. Your best bet is to use the example that Edneesis showed.
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
|