1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

someone can help me with multithread?

Discussion in 'Visual Basic .NET' started by zx123, Nov 9, 2012.

  1. zx123

    zx123 Senior Member

    Joined:
    Feb 26, 2009
    Messages:
    1,159
    Likes Received:
    876
    Home Page:
    someone know how to run X thread at the same time?

    example:
    I have a big list of items and I need to execute a function on every item.
    So if I set to run X thread at the same time and I will execute the function on the on the X items etc..
    Like scrapebox do when check pagerank or other things.
    I tried everything, I read a lot of documentation but nothing
    please help me with some examples
    I use visual basic .net
     
  2. kill4

    kill4 Regular Member

    Joined:
    May 11, 2009
    Messages:
    243
    Likes Received:
    42
    Location:
    Texas
    How i do my threading, is create a class. For example i have a httpwebrequest class. I create properties in the class that hold values for example, a url, proxy ect. The most important property in the class would be a property called index. The index property would keep track of what thread is using the class, by the index.

    The class would also have public events. Say i have an event in my class called DataArrival(Data as string,Index as integer)
    we could raise that event when we get data back from a webrequest with the content we got back from the website, and the index that requested it, Witch would be the thread number that requested it. This way we know what thread requested what and each thread has its own information. How would you use the class? Example below.



    Imports System.Threading

    Public Class frmMain

    Private objRequest(99) As Request '-- Request is the name of our class that holds the properties, events ect..

    Private Sub StartThreads()
    For I as integer = 0 to 99
    objRequest(I) = New Request
    objRequest(I).Index = Index
    objRequest(I).Url = "http://www.yahoo.com/"

    '-----------------------------------------------------------------------------------------------------
    '-- Handlers that we added to process events that happen in our class
    AddHandler objRequest(I).DataArrival, AddressOf Data
    AddHandler objRequest(I).WebExecptionError, AddressOf WebExceptionError
    AddHandler objRequest(I).OtherError, AddressOf OtherError
    AddHandler objRequest(I).ConnectionTimedOut, AddressOf TimedOut
    '-----------------------------------------------------------------------------------------------------

    Dim A As Thread = New Thread(AddressOf objRequest(I).GetRequest) '-- GetRequest is a sub in the class that request a website. in this case we set it to get yahoo.com
    A.Start()

    Next
    End Sub

    Private Sub Data(Data as string,Index as integer)
    debug.writeline("Thread " & Index & " Got back data! And the content is " & Data)
    End Sub

    Private Sub WebExceptionError(Message as string,Index as integer)
    debug.writeline("Thread " & Index & " Got a error. Error is " & Message)
    End Sub

    Private Sub OtherError(Message as string,Index as integer)
    debug.writeline("Thread " & Index & " Got a error. Error is " & Message)
    End Sub

    Private Sub TimedOut(Index as integer)
    debug.writeline("Thread " & Index & " Connection timedout.")
    End Sub

    End Class

    Here we fired of 100 threads, with each thread having its own cookies, content ect. In each sub you can do what you want next with the thread preserving its own thread data. That's just an example if you got vb.net down good enough you will be able to go from there.
     
    • Thanks Thanks x 5
  3. HatIsBlack

    HatIsBlack Regular Member

    Joined:
    Sep 17, 2010
    Messages:
    265
    Likes Received:
    187
    Location:
    Where i belong
    One thing you must not forget is that you can never have more threads actively running then you have cores in your computer. Double it if it has HT technology. Now you may think I talk bullshit because you can run scrapebox with 400 threads if you like. That is true but the fact is that scrapebox and other internet software uses mechanisms that goes to sleep while it Waits for network to respond and so your computer moves on to the next thread meanwhile. In .net such classes can be webrequest operations and other network stream operations.
    So what was the point of this. You can never load a bunch of threads to do mathematical operations or similar since they have no sleep state that lets other threads take over. If you don't put a sleep manually in them
     
  4. dummydecoy

    dummydecoy Junior Member

    Joined:
    Jul 4, 2010
    Messages:
    154
    Likes Received:
    39
    OS already have the capability to schedule threads
    it doesn't depend on number of cores
    you can create as many threads as you like and the OS will be able to run them
    cmiiw
     
    • Thanks Thanks x 1
  5. hatemachine

    hatemachine Regular Member

    Joined:
    Jan 14, 2011
    Messages:
    271
    Likes Received:
    1,138
    So much wrong in one post. So, much, wrong.

    [​IMG]

    Using the ThreadPool you can start already spawned threads, which improves performance.
     
    Last edited: Feb 9, 2013
  6. HatIsBlack

    HatIsBlack Regular Member

    Joined:
    Sep 17, 2010
    Messages:
    265
    Likes Received:
    187
    Location:
    Where i belong
    Alright

    I give up. Not so mutch wrong. most of it is correct but OS have abilliy to put threads in sleep by itself.
    I made a test because of dummydecoy's post and i stand corrected.

    However if you do not put sleep in the threads your computer will not be usable very quick. I made a test on a dualcore machine and it started up about 30threads in about 2-3min. Every thread gets slower to start.
    I would still follow the rule that you should not exceed the threads then there is cores unless you have sleep events or use classes that makes your thread sleep. If you do not fire up more threads then you have cores your will loose performance on non sleeping threads.

    Here is a test code for 400 threads with no sleep and endless loops. Add a textbox and a button to make it run. This will still prove my previous post to have a good point. Even if "you can not" part was wrong.

    Code:
    Imports System.Threading
    Public Class Form1
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            Dim Threads(399) As worker
            Dim trd As Thread
            For t As Integer = 0 To 399
                Threads(t) = New worker
                trd = New Thread(AddressOf Threads(t).Thread)
                trd.IsBackground = True
                trd.Start()
                Do
                    If Threads(t).signal = 1 Then Exit Do
                    Application.DoEvents()
                Loop
                TextBox1.AppendText(t & vbCrLf)
            Next
        End Sub
    End Class
    Public Class worker
        Public signal As Integer = 0
        Public Sub Thread(ByVal junk As Object)
            Dim x As Long = 0
            signal = 1
            Do
            Loop
        End Sub
    End Class
    
    
    
    
     
  7. HatIsBlack

    HatIsBlack Regular Member

    Joined:
    Sep 17, 2010
    Messages:
    265
    Likes Received:
    187
    Location:
    Where i belong

    Threadpool is great. I agree. but even then if you fill your threads with non sleep code you will go stale.
     
  8. hatemachine

    hatemachine Regular Member

    Joined:
    Jan 14, 2011
    Messages:
    271
    Likes Received:
    1,138
    That's a poor example.

    Application.DoEvents should _not_ be used.

    Creating threads is an expensive job, thus the ThreadPool should be used. Sleeping a thread is bad and not needed, unless you want to delay some action. Context switches are also expensive, so spawning many threads is not suggestable, more is not always better. But if you need 2 mins to start 30 threads, then your code is really, really bad. I can start hundred of threads within seconds.
     
  9. HatIsBlack

    HatIsBlack Regular Member

    Joined:
    Sep 17, 2010
    Messages:
    265
    Likes Received:
    187
    Location:
    Where i belong
    Doevents should not be used. I know. but instead of fire a thread away from the gui i choose doevents in this example to prove a Point. Now you can fire 100s of threads that has no sleep in it like mine. Do it and prove me wrong. I can also optimze the code above to be much much better. but the code i wrote is very simple and proves a Point.
     
  10. hatemachine

    hatemachine Regular Member

    Joined:
    Jan 14, 2011
    Messages:
    271
    Likes Received:
    1,138
    Not exactly sure what you're asking of me, and not even sure what you are trying to prove. But starting 30 threads should not take 2 minutes.
     
  11. HatIsBlack

    HatIsBlack Regular Member

    Joined:
    Sep 17, 2010
    Messages:
    265
    Likes Received:
    187
    Location:
    Where i belong
    What i mean is. if i start a thread that runs and have no sleep event in it it will consume ALOT of cpu. sure i can use threadpool and fire away 1000 threads or skip the wait event in my code above to start all 400 in a go. but all Those threads will not acutally start unless other threads go to sleep. Either with help from os or from some sleep system within the thread. They will only fall into a queue that is processed by os when there it time to work with them. My code above was slow because i waited for each and one thread to start Before i started Another. maybe it was not 2 minutes but it feelt like it.. i did not really Clock it and it will be different from computer to computer. i have written software that makes use of several hundreds of threads at the same time and work well and fast. however they do have classes in them that will put them in wait mode. Like when you use httpwebrequest and such to get / post data. So what i asked of you is to prove me wrong with some code that can run fast and simultainious without sleep in it.
     
  12. hatemachine

    hatemachine Regular Member

    Joined:
    Jan 14, 2011
    Messages:
    271
    Likes Received:
    1,138
    It will do that if you are doing it wrong. Multithreading is hard to get right.

    [​IMG]

    Here is my latest app running 100 concurrent registrations(100 threads + 16 threads assigned by the .NET framework on startup ) with 2% CPU usage. Surely that's not A LOT of CPU, not a single time Sleep is being called in it. Maybe you should look up async web requests, should help you out. :)
     
  13. HatIsBlack

    HatIsBlack Regular Member

    Joined:
    Sep 17, 2010
    Messages:
    265
    Likes Received:
    187
    Location:
    Where i belong
    asyc in theory fireing threads in itslef and are in sleep mode while waiting for data. but you will never see it so that is a bad example. I clearly see that we never will come to an understanding. hardware can not be tricked. OS have ability to force sleep threads so it can mange more but perfomance will suffer. In my first post i was talking about mathematical operations and threads without sleep. If you do async you fire away code FROM your thread.(or where do you Think it will be executed?). Try make some calcualtions for a 3d landscape. fire up some calcualtions and you can never do better with more threads then there is cores.
     
    • Thanks Thanks x 1
  14. hatemachine

    hatemachine Regular Member

    Joined:
    Jan 14, 2011
    Messages:
    271
    Likes Received:
    1,138
    Indeed. Let's just agree to disagree. But one last thing, sure it's different when doing calculations, but not when it comes to automating websites.
     
  15. jazzc

    jazzc Moderator Staff Member Moderator Jr. VIP

    Joined:
    Jan 27, 2009
    Messages:
    2,468
    Likes Received:
    10,148
    HatIsBlack is correct.

    When you are working with heavy processor usage functionality (e.g. compiling), the maximum number of threads that will optimize your speed is equal to the number of cores (real or if you have HT, virtual).

    The reason is obvious - if you have 8 workers (cores), and you assign only 2 jobs in total, you have 6 of them sitting. If you assign 8 jobs total, they are all working on 1 job at the same time. If you assign 16 jobs in total, they still can only work on 1 job at a time, so the rest simple get queued. No extra gain above 8 but rather a small loss sine you have to manage the queue.

    What hatemachine has in mind is that he gets better performance when he uses say 100 threads in his program that 8. That 's true also, but the difference is that his program is not heavy processor usage. When you use the network, it 's may orders of magnitude slower that your cores. Which means that a task gets to sleep anyway while waiting the network IO. So in that case, it 's optimal to employ more threads than your cores.
     
    • Thanks Thanks x 3
  16. hatemachine

    hatemachine Regular Member

    Joined:
    Jan 14, 2011
    Messages:
    271
    Likes Received:
    1,138
    Indeed. I never said that he is not, my point was that it's different when doing heavy I/O work as you just confirmed.

    http://blogs.technet.com/b/markrussinovich/archive/2009/07/08/3261309.aspx is a good read
     
    Last edited: Feb 9, 2013
  17. terebi

    terebi Jr. VIP Jr. VIP Premium Member

    Joined:
    Apr 11, 2011
    Messages:
    337
    Likes Received:
    62
    Since new release of .Net 4.

    Instead of using Threads. You should look at using Tasks library.
    http://msdn.microsoft.com/en-us/library/dd235608.aspx

    Tasks automatically schedule itself to make the most use of what ever the underlying CPU architecture is.

    It also supports a lot of fancy things that make synchronisation between threads easier etc...