Hi all,
I cannot seem to solve this problem: I am attempting to generate a
unique filename with the help of the Random Number Generator
(RND.Next.ToString). All seems to work fine except that one third of the
time, I get duplicate file names; this poses a critical problem for my
application. The problem only seems to happen when Method1 calls Function2
to generate and return the random file name. If I move the logic to generate
the filename into Method1, it works properly. I have tried to use SyncLock,
etc. but to no avail.
Why is it that when Method1 calls Function2 to generate the random file
name, I get duplicates, whereas if I move the logic from Function2 into
Method1, I get no duplicate file names. Is this due to threading? I am lost
as to how to solve this seemingly simple problem.
Note: I have also tried the above replacing the Random Number
Generator with the Now.Second and Now.MilliSecond but with the exact same
results. It only works if it is done within the same method that is doing my
actual processing.
Regards,
Giovanni
Jon Skeet [C# MVP] - 21 Jan 2007 09:55 GMT
> I cannot seem to solve this problem: I am attempting to generate a
> unique filename with the help of the Random Number Generator
[quoted text clipped - 9 lines]
> Method1, I get no duplicate file names. Is this due to threading? I am lost
> as to how to solve this seemingly simple problem.
You're almost certainly creating a new instance of Random each time,
which is a mistake - you'll end up using the same seed several times,
giving you the same numbers, and therefore the same filename.
Create a single instance of Random and use it repeatedly. Note that
Random isn't thread-safe, so you shouldn't use it from multiple threads
without some sort of locking.
You might want to have a look at my StaticRandom class to simplify
things:
http://www.pobox.com/~skeet/csharp/miscutil

Signature
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Giovanni - 21 Jan 2007 17:32 GMT
Hi Jon,
Thanks for the info. I really appreciate it. I will try to implement
your solution. That aspect of the Random Number Generator had eluded me. Is
there a difference between using the Lock and the SyncLock objects? Is one
better than the other?
Beat Regards,
Giovanni
> > I cannot seem to solve this problem: I am attempting to generate a
> > unique filename with the help of the Random Number Generator
[quoted text clipped - 22 lines]
>
> http://www.pobox.com/~skeet/csharp/miscutil
Jon Skeet [C# MVP] - 21 Jan 2007 19:17 GMT
> Thanks for the info. I really appreciate it. I will try to implement
> your solution. That aspect of the Random Number Generator had eluded me. Is
> there a difference between using the Lock and the SyncLock objects? Is one
> better than the other?
"lock" is the C# equivalent of SyncLock (and they're statements, not
objects). They do the same thing - syntactic sugar around Monitor.Enter
and Monitor.Exit, basically.

Signature
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Giovanni - 21 Jan 2007 19:37 GMT
Hi Jon,
Thanks for the info. I had justed posted my reply when I discovered
what you stated in your response; I overlooked it. Thanks again for the help.
Regards,
Giovanni
> > Thanks for the info. I really appreciate it. I will try to implement
> > your solution. That aspect of the Random Number Generator had eluded me. Is
[quoted text clipped - 4 lines]
> objects). They do the same thing - syntactic sugar around Monitor.Enter
> and Monitor.Exit, basically.
Steve Drake - 22 Jan 2007 10:45 GMT
Would a GUID be off use?
or even better a SequentialGuid?
public static class SequentialGuids
{
[DllImport("rpcrt4.dll", SetLastError = true)]
static extern int UuidCreateSequential(out Guid guid);
public static Guid NewGuid()
{
Guid rval;
UuidCreateSequential(out rval);
return rval;
}
}
You can also use BITS of this GUID to create a Unique file.
Guids are unique across ALL PCs, so if you just want it unique for your
server you dont need to whole number.
Steve
> Hi all,
>
[quoted text clipped - 26 lines]
>
> Giovanni
Paul - 23 Jan 2007 09:02 GMT
Giovanni,
Steve is right, I always use Guids now when creating files that need
unique naming.
In it's simplest form
// Create the output file
StreamWriter swOut = File.CreateText(folder + "\\" +
Guid.NewGuid().ToString() + ".txt");