Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 279987
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T05:04:45+00:00 2026-05-12T05:04:45+00:00

Before I start, I want to point out that I’m pretty sure this actually

  • 0

Before I start, I want to point out that I’m pretty sure this actually happened. All my logs suggest that it did.

I’d like to know whether I’m wrong and this is impossible, whether it’s just incredibly unlikely (which I suspect), or if it’s not that unlikely and I’m doing something fundamentally wrong.

I have 4 instances of the same code running as Windows Services on the same server. This server has a multicore (4) processor.

Here’s a summary of the code:

public class MyProcess
{
    private System.Timers.Timer timer;

    // execution starts here
    public void EntryPoint()
    {
        timer = new System.Timers.Timer(15000);  // 15 seconds
        timer.Elapsed += new System.Timers.ElapsedEventHandler(Timer_Elapsed);
        timer.AutoReset = false;

        Timer_Elapsed(this, null);
    }

    private void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
    {
        string uid = GetUID();

        // this bit of code sends a message to an external process.
        //  It uses the uid as an identifier - these shouldn't clash!
        CommunicationClass.SendMessage(uid);

        timer.Start();
    }

    // returns an 18 digit number as a string
    private string GetUID()
    {
        string rndString = "";
        Random rnd = new Random((int)DateTime.Now.Ticks);
        for (int i = 0; i < 18; i++)
        {
            rndString += rnd.Next(0, 10);
        }
        return rndString;
    }

The external process that receives these messages got confused – I think because the same uid came from two separate processes. Based on that, it appears that the GetUID() method returned the same “random” 18 digit string for two separate processes.

I’ve seeded the Random class using DateTime.Now.Ticks which I thought would provide protection between threads – one tick is 100 nanoseconds, surely two threads couldn’t get the same seed value.

What I didn’t account for obviously is that we’re not talking about threads, we’re talking about processes on a multicore processor. That means that this code can literally run twice at the same time. I think that’s what’s caused the clash.

Two processes running the same code at approximate 15 second intervals managed to hit the same code inside 100 nanoseconds. Is this possible? Am I on the right track here?

I’d be grateful for your thoughts or suggestions.


To clarify, I can’t really use a GUID – the external process I’m communicating with needs an 18 digit number. It’s old, and I can’t change it unfortunately.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T05:04:45+00:00Added an answer on May 12, 2026 at 5:04 am

    You don’t want random numbers for this purpose, you want unique numbers. I’m with @JP. I think you should look at using GUIDs for your message ids.

    EDIT: if you can’t use a GUID, then think of a way to get a 64-bit number that is unique and use successive 3-bit chunks of it as an index into a 8-character alphabet (tossing the unused upper bits). One way to do this would be to have a database in which you create an entry for each new message with an auto-incremented 64-bit integer as the key. Use the key and translate it into your 18-character message id.

    If you don’t want to rely on a database you can get something that works under certain conditions. For example, if the messages only need to be unique during the life of the processes, then you could use the process id as 32 bits of the value and get the remaining 22 required bits from a random number generator. Since no two processes running at the same time can have the same id, they should be guaranteed to have unique message ids.

    There are undoubtedly many other ways that you could do this if your situation doesn’t fit into one of the above scenarios.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

before I start I want to point out that I tagged this question as
So, to start off, I want to point out that I know that these
First off, before I ask, i would like to point out that this question
Before I start, I want to make it clear that my code is working
I want to get one record before start date and end date DtpFrom means
Am using Mediainfo library in my C# project,before start invoking this dll,i just ran
Google results on this one are a bit thin, but suggest that it is
I'm extremely familiar with regex before you all start answering with variations of: /d+
I read all Questions on stackoverflow that could give me a hint with this
Before start let me tell my experience: I am experienced with C#.NET, web services,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.