I needed to generate some unique number in my application. I could use GUID, but it was too large for me (I need to keep lots of unique identifiers). I found something like this:
[System.Runtime.InteropServices.DllImport("Kernel32.dll")]
private static extern int QueryPerformanceFrequency(ref System.Int64 frequency);
[System.Runtime.InteropServices.DllImport("Kernel32.dll")]
private static extern int QueryPerformanceCounter(ref System.Int64 performanceCount);
public static long GenerateUniqueId()
{
System.Int64 id = 0;
QueryPerformanceFrequency(ref id);
QueryPerformanceCounter(ref id);
return id;
}
This code generates Int64 (long) unique number (at least I hope it is unique). The uniqueness is in the scope of process. So two processes can generate the same number, but it should be always unique in a single process (I am not sure about two threads invoking the same GenerateUniqueId() method.