Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2016
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.92 KB | None | 0 0
  1. private class NativeMethods
  2. {
  3. [DllImport("rpcrt4.dll", SetLastError = true)]
  4. public static extern int UuidCreateSequential(out Guid guid);
  5. }
  6.  
  7. public static Guid CreateSequentialGuid()
  8. {
  9. const int RPC_S_OK = 0;
  10.  
  11. Guid guid;
  12. int result = NativeMethods.UuidCreateSequential(out guid);
  13. if (result == RPC_S_OK)
  14. return guid;
  15. else throw new Exception("could not generate unique sequential guid");
  16. }
  17.  
  18. static void TestSortedSequentialGuid(int length)
  19. {
  20. Guid[] guids = new Guid[length];
  21. int[] ids = new int[length];
  22.  
  23. for (int i = 0; i < length; i++)
  24. {
  25. guids[i] = CreateSequentialGuid();
  26. ids[i] = i;
  27. //Thread.Sleep(60000);
  28. }
  29.  
  30. Array.Sort(guids, ids);
  31.  
  32. for (int i = 0; i < length - 1; i++)
  33. {
  34. if (ids[i] > ids[i + 1])
  35. {
  36. Console.WriteLine("sorting using guids failed!");
  37. return;
  38. }
  39. }
  40. foreach (var guid in guids)
  41. {
  42. Console.WriteLine(guid);
  43. }
  44. Console.WriteLine("sorting using guids succeeded!");
  45. }
  46. public Form1()
  47. {
  48.  
  49. InitializeComponent();
  50.  
  51. TestSortedSequentialGuid(10);
  52. }
  53.  
  54.  
  55. Produces:
  56.  
  57. 372ff0ed-505a-11e6-8393-50e549349cd5
  58. 372ff0ee-505a-11e6-8393-50e549349cd5
  59. 372ff0ef-505a-11e6-8393-50e549349cd5
  60. 372ff0f0-505a-11e6-8393-50e549349cd5
  61. 372ff0f1-505a-11e6-8393-50e549349cd5
  62. 372ff0f2-505a-11e6-8393-50e549349cd5
  63. 372ff0f3-505a-11e6-8393-50e549349cd5
  64. 372ff0f4-505a-11e6-8393-50e549349cd5
  65. 372ff0f5-505a-11e6-8393-50e549349cd5
  66. 372ff0f6-505a-11e6-8393-50e549349cd5
  67. sorting using guids succeeded!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement