static void Main(string[] args)
{
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
int n = 0;
for (int i = 1; i <= number_of_students; i++)
{
new Thread(interview).Start(i);
n++;
}
//I want to show execution time after all threads finish execution
Console.WriteLine("Elapsed Time is {0} ms", stopwatch.ElapsedMilliseconds);
}
Asked
Active
Viewed 84 times
0

Peter Duniho
- 68,759
- 7
- 102
- 136

King
- 1
-
You probably need the [`Thread.Join`](https://learn.microsoft.com/en-us/dotnet/api/system.threading.thread.join) method. *Blocks the calling thread until the thread represented by this instance terminates.* You need to store all threads in an array, and then loop and `Join` the current thread with each one of them. – Theodor Zoulias Apr 01 '20 at 04:41
-
@mjwills the question referred as [duplicate](https://stackoverflow.com/questions/2773479/how-to-check-if-thread-finished-execution) is a quite different question. "Check" and "wait" is not the same thing. A better match is probably this: [How to wait for thread to finish with .NET?](https://stackoverflow.com/questions/1584062/how-to-wait-for-thread-to-finish-with-net) – Theodor Zoulias Apr 01 '20 at 04:48
-
2I don't disagree @TheodorZoulias - but the _answers_ in the duplicate were definitely applicable to the OP. Were there better ones? Surely. Heaps of them - if only they'd searched. :) – mjwills Apr 01 '20 at 05:48