1

I am creating threads and each thread will do some task and write consumed time; which it takes to complete a task, in a single file. I cannot use locks because if a thread go into waiting state I cannot measure exact time the thread takes to complete the task.

Is there any solution that I can track the exact time taken by threads to complete the task. Task is to run a function having some time-consuming work.

Ahsan
  • 648
  • 1
  • 10
  • 27
  • What do you mean by "which it takes to complete a task, in a single file"? What do the threads do? Some work and then write into the file? Or the work is to write in the file? You can "serialize" the writes in a single thread. – Stelios Adamantidis Jan 24 '14 at 07:02
  • The title says "...threads...file...locks"; the description says "...measure time taken..."; the tag is generic "multithreading". Please, consider editing the question. – Max Yakimets Jan 24 '14 at 07:11
  • See Jon Skeet's answer http://stackoverflow.com/questions/252793/timing-a-line-of-code-accurately-in-a-threaded-application-c-sharp – Max Yakimets Jan 24 '14 at 07:20
  • possible duplicate of [How do you measure code block (thread) execution time with multiple concurrent threads in .NET](http://stackoverflow.com/questions/375068/how-do-you-measure-code-block-thread-execution-time-with-multiple-concurrent-t) – Max Yakimets Jan 24 '14 at 07:20

2 Answers2

2

You can write to file asynchronously. In this case, your threads call another Task that will write to file and continue execution, and this another Task will wait for lock.

Another solution - you can use queue (MSMQ as example) to send logging message and writing to file will be from single thread on another end of queue.

Kirill Bestemyanov
  • 11,946
  • 2
  • 24
  • 38
1

Use the Ticks property to get the most fine-grained level of detail. A tick is 100ns, so divide by 10 to get to microseconds.

Ansh David
  • 654
  • 1
  • 10
  • 26