Results 1 to 11 of 11

Thread: Multithreaded program

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2000
    Location
    Texas
    Posts
    313

    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.

  2. #2
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    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?

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2000
    Location
    Texas
    Posts
    313
    My question is. Can vb execute more then one line of code at a time.

  4. #4
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    lol yes it can. use the System.Threading namespace...and the class Thread
    \m/\m/

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2000
    Location
    Texas
    Posts
    313
    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.

  6. #6
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    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

  7. #7
    Frenzied Member
    Join Date
    Aug 2000
    Location
    Birmingham, AL
    Posts
    1,276
    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.

  8. #8
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Would this make any sense ? .
    VB Code:
    1. Public Sub thread1()
    2.         MessageBox.Show("Thread 1 fired")
    3.     End Sub
    4.  
    5.     Public Sub thread2()
    6.         MessageBox.Show("Thread 2 fired")
    7.     End Sub
    8.  
    9.     Public Sub AllThread()
    10.         'Combine the two subs in one sub so they can be
    11.         'fired at once
    12.         Me.thread1()
    13.         Me.thread2()
    14.     End Sub
    15.  
    16.     Private Sub Button1_Click(ByVal sender As System.Object,
    17. ByVal e As System.EventArgs) Handles Button1.Click
    18.         Dim thrd As Threading.Thread
    19.         thrd = New Threading.Thread(AddressOf AllThread)
    20.         thrd.Start()
    21.  
    22.     End Sub

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2000
    Location
    Texas
    Posts
    313
    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.

  10. #10
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Here is an example using delegates:
    VB Code:
    1. Public Delegate Function MessageBoxShowHandler(ByVal test As String) As DialogResult
    2.  
    3.     Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    4.         Dim del As New MessageBoxShowHandler(AddressOf MessageBox.Show)
    5.         del.BeginInvoke("Thread 1 fired", Nothing, Nothing)
    6.         del.BeginInvoke("Thread 2 fired", Nothing, Nothing)
    7.         MessageBox.Show("Notice that the other messageboxes didn't stop execution of this thread since they are on their own thread.")
    8.         MessageBox.Show("I got held up because the last messagebox wasn't on a different thread.")
    9.     End Sub
    Last edited by Edneeis; Jul 16th, 2003 at 09:34 PM.

  11. #11
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    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
  •  



Click Here to Expand Forum to Full Width