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

C# syncronizing threads

Discussion in 'C, C++, C#' started by ilPatrino, Apr 14, 2012.

Tags:
  1. ilPatrino

    ilPatrino Junior Member

    Joined:
    Sep 6, 2011
    Messages:
    130
    Likes Received:
    15
    Hey Guys I've been working on a bot and stumbled upon a problem. I have a function that is called by multiple threads, now I want to run another function as soon as ALL the threads have finished.

    I tried searching online for this & there seems to be multiple solutions like WaitHandles,Join etc etc.... The thing is that when I try to implement these the UI freezes.

    Have you got any idea of a solution for this problem, I'm using Normal Threads btw as I can control how many threads each function gets.
     
  2. blancog76

    blancog76 Registered Member

    Joined:
    Jun 25, 2011
    Messages:
    74
    Likes Received:
    16
    Occupation:
    Mad Coder
    just use a mutex and the waitall function
     
  3. healzer

    healzer Jr. Executive VIP Jr. VIP Premium Member

    Joined:
    Jun 26, 2011
    Messages:
    2,363
    Likes Received:
    1,966
    Gender:
    Male
    Occupation:
    Marketing automation tools
    Location:
    Somewhere in Europe
    Home Page:
    I'm a vb coder, but u can make a boolean for each thread.
    when all the booleans are set to true (=when a thread finishes)

    then a timer will check if they are all on true -> if they are -> continue...
     
  4. ilPatrino

    ilPatrino Junior Member

    Joined:
    Sep 6, 2011
    Messages:
    130
    Likes Received:
    15
    1.Wait all will block the UI

    2. I like the idea, gonna try it, thanks mate
     
  5. m4dm4n

    m4dm4n Jr. VIP Jr. VIP Premium Member

    Joined:
    Sep 15, 2010
    Messages:
    221
    Likes Received:
    92
    Occupation:
    /dev/full
    Location:
    /dev/urandom
    if you want to keep the ui from blocking.... start a master thread (besides the normal thread) and do your work from there and leave the main thread(ui) alone or... don't use blocking functions in the main thread as those are the ones freezing your ui
     
  6. blancog76

    blancog76 Registered Member

    Joined:
    Jun 25, 2011
    Messages:
    74
    Likes Received:
    16
    Occupation:
    Mad Coder
    as mdm4n stated, you should be spawning off a controller thread that does the waitall - it should not be in a UI event. WaitAll is optimized at the IL layer as opposed to creating arrays of booleans where a timer kicks in ... sometimes you can get something to work and it is elegant too
     
  7. kill4

    kill4 Regular Member

    Joined:
    May 11, 2009
    Messages:
    243
    Likes Received:
    42
    Location:
    Texas
    Im a vb.net programmer to, but you can try this logic in your program. What i usually do is keep track of the thread count in a sub or function and use the Increment method of the Interlocked class. Something like this for example..

    Code:
    Private Sub ExampleSub()
    Interlocked.Increment(ThreadFinished)
    If ThreadFinished >=  TotalThreads Then
         MessageBox.Show("All Threads Finished.")
         return
    End If
    End Sub
    
    If you don't know much about the Interlocked.Increment method is basically blocks all other threads, from accessing a variable tell its done with it, so it comes in handy with keeping count of a variable that needs to be incremented by more then one thread.

    This is a good read about it http://www.developerfusion.com/article/5184/multithreading-in-vbnet/2/ . It's a vb.net tutorial, but you will get a feel for what it does by reading it.
     
  8. ilPatrino

    ilPatrino Junior Member

    Joined:
    Sep 6, 2011
    Messages:
    130
    Likes Received:
    15
    Do you have any useful example you could link me to?
     
  9. Aremys

    Aremys Regular Member

    Joined:
    Sep 13, 2008
    Messages:
    307
    Likes Received:
    72
    You need to use the BackgroundWorker class to prevent the UI from freezing.
     
  10. smack

    smack Junior Member

    Joined:
    Feb 1, 2010
    Messages:
    182
    Likes Received:
    78
    Occupation:
    Software Engineer/Evil Genius
    Location:
    inside .NET
    what's probably happening here is that your caller is executing in the UI code, that's why it freezes. the first thing i generally do with a multithreaded application is spawn off one thread from the main UI thread to act as the starter. this thread then loops to a specified number and spawns off all the other worker threads, then dies.

    beyond that your synchronization can take many forms, which depends on what exactly you're trying to do. you have your normal lock() statements, mutex, Semaphor, SemaphorSlim, etc...

    also keep in mind that if you're trying to directly update the UI from the background threads you need to be using the controls Invoke() methods to avoid cross thread exceptions. i actually wrote a post on Invoke() a while back. if you search it should come up.
     
  11. theMagicNumber

    theMagicNumber Regular Member

    Joined:
    May 13, 2010
    Messages:
    345
    Likes Received:
    195
    Code:
                private void MainWorker()
                {[INDENT]            List<Thread> threads= new List<Thread>();[/INDENT]
    [INDENT]            for (int i = 0; i < 10; i++)[/INDENT]
    [INDENT]            {[/INDENT]
    [INDENT]                Thread tr = new Thread(SomeFunction);[/INDENT]
    [INDENT]                threads.Add(tr);[/INDENT]
    [INDENT]                tr.Start();[/INDENT]
    [INDENT]            }[/INDENT]
    [INDENT]            foreach (Thread tr in threads)[/INDENT]
    [INDENT]            {[/INDENT]
    [INDENT]                tr.Join();[/INDENT]
    [INDENT]            }[/INDENT]
    [INDENT=2]    //All threads completed their work do whatever you need[/INDENT]
                }
    
    Just call the function using BackgroundWorker, Thread or ThreadPool
    For example:
    Code:
    [INDENT=2]ThreadPool.QueueUserWorkItem(new WaitCallback(MainWorker));
    // This won't freeze the GUI[/INDENT]
    
     
    Last edited: Apr 15, 2012