I am using Following .NET Code
Class MonitorSample
Shared Sub RunMonitor()
Dim o As New Object()
For i As Integer = 0 To 99
ThreadPool.QueueUserWorkItem(Function()
Try
Monitor.Enter(o)
Console.WriteLine("Thread {0} acquired lock...working", Thread.CurrentThread.ManagedThreadId)
Console.WriteLine("Thread {0} performing some I/O operation so yielding the lock temporarily...", Thread.CurrentThread.ManagedThreadId)
Monitor.PulseAll(o)
Monitor.Wait(o)
Console.WriteLine("Thread {0} reacquired lock", Thread.CurrentThread.ManagedThreadId)
Finally
Console.WriteLine("Thread {0} released lock", Thread.CurrentThread.ManagedThreadId)
Monitor.PulseAll(o)
Monitor.Exit(o)
End Try
Return Nothing
End Function
)
Next
Console.ReadLine()
End Sub
Shared Sub Main()
Dim stopwatch As New Stopwatch()
stopwatch.Start()
RunMonitor()
stopwatch.Stop()
Console.WriteLine("Time elapsed: {0}", stopwatch.Elapsed)
End Sub
End Class
In the main method is this the right method to calculate time consuemd by thread or we should calculate in some otherway.
Actually the step to print time consumed get printed first while threads get executed later.
Your Sub won’t do as you want – there are 2 issues here
Console.Readlinewhich will also wait for the user’s input inside the StopWatchThe following should fix this by moving the stopwatch tightly around the threads, and also providing a CountDownEvent to allow each thread to indicate when it has completed.