Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private class NativeMethods
- {
- [DllImport("rpcrt4.dll", SetLastError = true)]
- public static extern int UuidCreateSequential(out Guid guid);
- }
- public static Guid CreateSequentialGuid()
- {
- const int RPC_S_OK = 0;
- Guid guid;
- int result = NativeMethods.UuidCreateSequential(out guid);
- if (result == RPC_S_OK)
- return guid;
- else throw new Exception("could not generate unique sequential guid");
- }
- static void TestSortedSequentialGuid(int length)
- {
- Guid[] guids = new Guid[length];
- int[] ids = new int[length];
- for (int i = 0; i < length; i++)
- {
- guids[i] = CreateSequentialGuid();
- ids[i] = i;
- //Thread.Sleep(60000);
- }
- Array.Sort(guids, ids);
- for (int i = 0; i < length - 1; i++)
- {
- if (ids[i] > ids[i + 1])
- {
- Console.WriteLine("sorting using guids failed!");
- return;
- }
- }
- foreach (var guid in guids)
- {
- Console.WriteLine(guid);
- }
- Console.WriteLine("sorting using guids succeeded!");
- }
- public Form1()
- {
- InitializeComponent();
- TestSortedSequentialGuid(10);
- }
- Produces:
- 372ff0ed-505a-11e6-8393-50e549349cd5
- 372ff0ee-505a-11e6-8393-50e549349cd5
- 372ff0ef-505a-11e6-8393-50e549349cd5
- 372ff0f0-505a-11e6-8393-50e549349cd5
- 372ff0f1-505a-11e6-8393-50e549349cd5
- 372ff0f2-505a-11e6-8393-50e549349cd5
- 372ff0f3-505a-11e6-8393-50e549349cd5
- 372ff0f4-505a-11e6-8393-50e549349cd5
- 372ff0f5-505a-11e6-8393-50e549349cd5
- 372ff0f6-505a-11e6-8393-50e549349cd5
- sorting using guids succeeded!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement