Like Tree3Likes
  • 1 Post By theoobe
  • 1 Post By itsme86
  • 1 Post By itsme86

Thread wait

This is a discussion on Thread wait within the C# Programming forums, part of the General Programming Boards category; Hi! After i've created a thread, Code: Thread thread = new Thread(new ThreadStart(alpha.ThreadProc)); how to wait for that thread to ...

  1. #1
    Android
    Join Date
    Mar 2004
    Location
    Kurunegala Colony, Sri Lanka, Sri Lanka
    Posts
    386

    Thread wait

    Hi!

    After i've created a thread,
    Code:
    Thread thread = new Thread(new ThreadStart(alpha.ThreadProc));
    how to wait for that thread to finish without using Sleep?

    thanks.
    Some useless programs i've wrote : slprogrammersheaven
    Youtube: http://www.youtube.com/user/geekyzine
    My amature IT Blog: http://everything-geeky.blogspot.com/
    My Flash games: http://flashweed.blogspot.com/

    (and, sorry for my amature english)

  2. #2
    Registered User
    Join Date
    Mar 2009
    Location
    england
    Posts
    209
    You could consider using the BackgroundWorker object which includes a convenient RunWorkerCompleted event which fires when the thread dies.
    geek@02 likes this.

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,171
    If you wish to stick with the Thread, you can call thread.Join() which will cause the calling thread to wait until the other thread completes, or you can poll thread.IsAlive.
    geek@02 likes this.
    If you understand what you're doing, you're not learning anything.

  4. #4
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    The better question is why you think you should wait. By spawning a thread and then waiting until it completes, you basically remove the benefits of spawning the thread in the first place. In general you should try to avoid needing to do this except under exceptional circumstances.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  5. #5
    Android
    Join Date
    Mar 2004
    Location
    Kurunegala Colony, Sri Lanka, Sri Lanka
    Posts
    386
    Quote Originally Posted by Cat View Post
    The better question is why you think you should wait. By spawning a thread and then waiting until it completes, you basically remove the benefits of spawning the thread in the first place. In general you should try to avoid needing to do this except under exceptional circumstances.
    Thanks.

    It's for an example program from a book that creates 100 threads in a loop, and each of those threads needs to read the same file and write back to it.
    Last edited by geek@02; 05-03-2012 at 09:10 PM.
    Some useless programs i've wrote : slprogrammersheaven
    Youtube: http://www.youtube.com/user/geekyzine
    My amature IT Blog: http://everything-geeky.blogspot.com/
    My Flash games: http://flashweed.blogspot.com/

    (and, sorry for my amature english)

  6. #6
    Android
    Join Date
    Mar 2004
    Location
    Kurunegala Colony, Sri Lanka, Sri Lanka
    Posts
    386
    ok i used isAlive and sleep:

    Code:
    for(int nTotal=3;  nTotal>0; --nTotal)
                {
                    Thread thread = new Thread(new ThreadStart(alpha.ThreadProc));
                    thread.Start();
                    while (thread.IsAlive)
                    {
                        Thread.Sleep(10);
                    }
                }
    Thread process is like this, which reads a whole file and write the read data back:

    Code:
    public void ThreadProc()
            {
                string path = "myfile.txt";
                n++;
                if (!File.Exists(path))
                {
                    fs = File.Create(path);
                }
                else
                {
                    contents = File.ReadAllText(path);
                    File.AppendAllText(path,contents);
                }
                
            }
    In the loop i create 3 threads. Initially myfile.txt contains "foobar", so after execution i expected to find "foobar" 3 times written back to file. But i got this: "foobar foobar foobar foobar foobar foobar foobar foobar ". What i'm doing wrong here?
    Some useless programs i've wrote : slprogrammersheaven
    Youtube: http://www.youtube.com/user/geekyzine
    My amature IT Blog: http://everything-geeky.blogspot.com/
    My Flash games: http://flashweed.blogspot.com/

    (and, sorry for my amature english)

  7. #7
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,171
    The results will be unpredictable, because there's no way to know when each thread will read the contents of the file. It might be that the first thread reads foobar, and then writes foobar foobar, and then the next thread reads foobar foobar and writes foobar foobar foobar, etc. You don't know which thread is doing what when.
    geek@02 likes this.
    If you understand what you're doing, you're not learning anything.

  8. #8
    Android
    Join Date
    Mar 2004
    Location
    Kurunegala Colony, Sri Lanka, Sri Lanka
    Posts
    386
    Quote Originally Posted by itsme86 View Post
    first thread reads foobar, and then writes foobar foobar, and then the next thread reads foobar foobar and writes foobar foobar foobar, etc.
    oh that's what happening. why did i expected it'll print only 3 times
    Some useless programs i've wrote : slprogrammersheaven
    Youtube: http://www.youtube.com/user/geekyzine
    My amature IT Blog: http://everything-geeky.blogspot.com/
    My Flash games: http://flashweed.blogspot.com/

    (and, sorry for my amature english)

  9. #9
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Quote Originally Posted by geek@02 View Post
    Thanks.

    It's for an example program from a book that creates 100 threads in a loop, and each of those threads needs to read the same file and write back to it.
    I'd say the example itself is subpar. In general you only really benefit from using threads when each thread's operations are nearly or totally independent. The more that threads need to use the same resources, the less of a performance boost you get because threads contend with each other for the resource.

    In your example, you're actually executing all three threads sequentially, not concurrently, because you wait until each thread terminates to continue the loop and kick off the next thread. There's no benefit at all in this example to using threads apart from learning thread syntax.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 11-20-2011, 12:01 AM
  2. Replies: 2
    Last Post: 07-01-2007, 08:11 AM
  3. pointer to main thread from worker thread?
    By draegon in forum C++ Programming
    Replies: 2
    Last Post: 10-27-2005, 07:35 AM
  4. Replies: 2
    Last Post: 04-12-2004, 02:37 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21