views:

57

answers:

3

I have two secondary threads,that executes the method WriteX as described below:

    static void Main(string[] args)
    {

        ThreadStart one = new ThreadStart(WriteX);
        Thread startOne = new Thread(one);
        ThreadStart two = new ThreadStart(WriteX);
        Thread startTwo = new Thread(two);
        startOne.Start();
        startTwo.Start();
        Console.ReadKey(true);
    }

    public static void WriteX()
    {
        for (int i = 1; i <= 3; i++)
        {
            Console.WriteLine("Hello");
            Thread.Sleep(1000);
        }
    }

( 1 ) How can i find the time taken (in milli seconds) by "startOne" and "startTwo" to complete its task?

( 2 ) I started the Thread (Start() ).Won't i need to stop the thread upon successful execution or will that be handled by the primary thread (Main() in this case) ?

(3) How can i print the message say , startOne is executing WriteX() method and startTwo is executing WriteX() method ?

A: 
  1. Wrap WriteX() in something that saves the current time before and after (or simply do it at the begin and end of WriteX()) and print the difference at the end.

  2. When the method passed to ThreadStart finishes, the thread terminates itself (and cleans itself up)

  3. Again, wrap WriteX() in something that does the logging. I suggest to look at the source for ThreadStart to see how it's implemented and how you can extend/wrap it.

If you have no idea what I'm talking about, this article might help.

Aaron Digulla
Thanks Aaron Digulla,I will do
A: 
  1. It depends on whether you want to get sum of execution times of two independent tasks or the biggest one. In first case you need to measure required time within WriteX. In the second case before startOne and stop measuring when both ar finished. Use Stopwatch in both cases.

  2. Use Thread.Join to wait until both threads are finished.

  3. Use parameterized start Thread.Start(object) and pass task name there.

Dzmitry Huba
Thank you Dzmitry Huba. :)
+1  A: 

In every serious logging framework, you can include the thread Id, or the thread name in the output message. If you just want to use the Console, you may have to manually append the current thread id/name to your message.

Like so:

public static void WriteX()
{
    var sw = Stopwatch.StartNew();
    for (int i = 1; i <= 3; i++)
    {
        Console.WriteLine("Hello");
        Thread.Sleep(1000);
    }
    sw.Stop();
    Console.WriteLine("{0} {1}", 
                      Thread.CurrentThread.ManagedThreadId, 
                      sw.Elapsed);
}

To name a thread, just use the Name property on the thread instance:

ThreadStart one = new ThreadStart(WriteX);
Thread startOne = new Thread(one);
startOne.Name = "StartOne";

//...

Console.WriteLine("{0} {1}", Thread.CurrentThread.Name, sw.Elapsed);

Also, when the method you pass as ThreadStart constructor argument finishes, the thread ends automatically. Use the Join method if you want to wait for a thread end in the main one.

Romain Verdier
Thank you Romain Verdier.Really useful.Once again thank you very much.