How do I write a timestamp that is more accurate than milliseconds?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • erotavlas
    New Member
    • Sep 2010
    • 2

    #1

    How do I write a timestamp that is more accurate than milliseconds?

    Hi,
    I have a program that is listening for messages on a port and writes each message to a separate file. I'm currently using the DateTime Now to write a time stamp into the file name so that each message has a unique name (i.e. doesn't get overwritten by the next)

    However some messages are getting overwritten by the next message since the time did not yet have a chance to advance to change the file name time stamp. (machine is very fast and messages are coming in quickly)

    For each message the program resets and so I cannot use a counter inside the program. and I cannot change this aspect of the program.

    Is there another way other than DateTime, to generate a unique number for each file that does not rely on a counter inside the program itself?

    Thanks for any help
  • Aimee Bailey
    Recognized Expert New Member
    • Apr 2010
    • 197

    #2
    DateTime has a more accurate measurement called .Ticks. This may not be the perfect answer, but it'll give you a more accurate timestamp.

    Aimee.

    Comment

    • GaryTexmo
      Recognized Expert Top Contributor
      • Jul 2009
      • 1501

      #3
      If it helps, there are 10 000 ticks in a millisecond.

      http://msdn.microsoft.com/en-us/libr...ime.ticks.aspx

      Comment

      • Christian Binder
        Recognized Expert New Member
        • Jan 2008
        • 218

        #4
        If all your files are in the same folder, you can check all the filenames of the files in this folder and create a filename, which wasn't used before.

        Or you use DateTime.Now.Ti cks as suggested.

        Or you can use Guid.NewGuid() which creates a globally unique identifier who can be used for creating a filename.
        (But this would cause unsorted filenames.

        You could also use a file where you are storing a counter and take this for the filename.

        Comment

        • erotavlas
          New Member
          • Sep 2010
          • 2

          #5
          Hey everyone, thanks for the suggestions. In the end I stuck with the milliseconds in the time stamp, but just added the NewGuid value as Christian suggested since all I cared about was preventing the files from getting overwritten, the file name itself wasn't too much of an issue.

          Comment

          Working...