Hi!
After i've created a thread,
how to wait for that thread to finish without using Sleep?Code:Thread thread = new Thread(new ThreadStart(alpha.ThreadProc));
thanks.
Hi!
After i've created a thread,
how to wait for that thread to finish without using Sleep?Code:Thread thread = new Thread(new ThreadStart(alpha.ThreadProc));
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)
You could consider using the BackgroundWorker object which includes a convenient RunWorkerCompleted event which fires when the thread dies.
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.
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.
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)
ok i used isAlive and sleep:
Thread process is like this, which reads a whole file and write the read data back:Code:for(int nTotal=3; nTotal>0; --nTotal) { Thread thread = new Thread(new ThreadStart(alpha.ThreadProc)); thread.Start(); while (thread.IsAlive) { Thread.Sleep(10); } }
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?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); } }
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)
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.
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)
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.