Thread: Thread wait

  1. #1
    Android geek@02's Avatar
    Join Date
    Mar 2004
    Location
    Kurunegala Colony, Sri Lanka, Sri Lanka
    Posts
    470

    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.
    GameJolt: https://gamejolt.com/@KasunL
    Game Development Youtube:
    https://is.gd/XyhYoP
    Amateur IT Blog: http://everything-geeky.blogspot.com/



    (and, sorry for my amateur 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.

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    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.
    If you understand what you're doing, you're not learning anything.

  4. #4
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    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 geek@02's Avatar
    Join Date
    Mar 2004
    Location
    Kurunegala Colony, Sri Lanka, Sri Lanka
    Posts
    470
    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 08:10 PM.
    GameJolt: https://gamejolt.com/@KasunL
    Game Development Youtube:
    https://is.gd/XyhYoP
    Amateur IT Blog: http://everything-geeky.blogspot.com/



    (and, sorry for my amateur English)

  6. #6
    Android geek@02's Avatar
    Join Date
    Mar 2004
    Location
    Kurunegala Colony, Sri Lanka, Sri Lanka
    Posts
    470
    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?
    GameJolt: https://gamejolt.com/@KasunL
    Game Development Youtube:
    https://is.gd/XyhYoP
    Amateur IT Blog: http://everything-geeky.blogspot.com/



    (and, sorry for my amateur English)

  7. #7
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    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.
    If you understand what you're doing, you're not learning anything.

  8. #8
    Android geek@02's Avatar
    Join Date
    Mar 2004
    Location
    Kurunegala Colony, Sri Lanka, Sri Lanka
    Posts
    470
    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
    GameJolt: https://gamejolt.com/@KasunL
    Game Development Youtube:
    https://is.gd/XyhYoP
    Amateur IT Blog: http://everything-geeky.blogspot.com/



    (and, sorry for my amateur English)

  9. #9
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    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, 07:11 AM
  3. pointer to main thread from worker thread?
    By draegon in forum C++ Programming
    Replies: 2
    Last Post: 10-27-2005, 06:35 AM
  4. Replies: 2
    Last Post: 04-12-2004, 01:37 AM