GlobalLogEntryCounter.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. using System;
  2. using System.IO;
  3. using System.Runtime.InteropServices;
  4. namespace Wayne.Lib.Log
  5. {
  6. internal class GlobalLogEntryCounter : IDisposable
  7. {
  8. #region Win32 Semaphore APIs
  9. [DllImport("kernel32", EntryPoint = "CreateSemaphore", SetLastError = true, CharSet = CharSet.Unicode)]
  10. private static extern uint CreateSemaphore(int auth, int initialCount, int maximumCount, string name);
  11. [DllImport("kernel32", EntryPoint = "OpenSemaphore", SetLastError = true, CharSet = CharSet.Unicode)]
  12. private static extern uint OpenSemaphore(int auth, bool bInheritHandle, string name);
  13. [DllImport("kernel32", EntryPoint = "ReleaseSemaphore", SetLastError = true, CharSet = CharSet.Unicode)]
  14. [return: MarshalAs(UnmanagedType.VariantBool)]
  15. private static extern bool ReleaseSemaphore(uint hHandle, int lReleaseCount, out int lpPreviousCount);
  16. [DllImport("kernel32", EntryPoint = "CloseHandle", SetLastError = true, CharSet = CharSet.Unicode)]
  17. [return: MarshalAs(UnmanagedType.VariantBool)]
  18. private static extern bool CloseHandle(uint hHandle);
  19. #endregion
  20. #region Fields
  21. private const string GlobalName = "Wayne.Lib.Log.GlobalLogEntryCounter";
  22. private bool disposed;
  23. private readonly bool fullWindows;
  24. private uint fullWindowsSemaphoreHandle;
  25. #endregion
  26. #region Construction
  27. /// <summary>
  28. /// Constructor.
  29. /// </summary>
  30. public GlobalLogEntryCounter()
  31. {
  32. fullWindows = Directory.Exists(@"C:\");
  33. if (fullWindows)
  34. {
  35. int suffix = 0;
  36. do
  37. {
  38. suffix++;
  39. var wayneLibLogGloballogentrycounter = GlobalName + "." + suffix;
  40. fullWindowsSemaphoreHandle = OpenSemaphore(0, true, wayneLibLogGloballogentrycounter);
  41. if (fullWindowsSemaphoreHandle == 0)
  42. fullWindowsSemaphoreHandle = CreateSemaphore(0, 1, int.MaxValue, wayneLibLogGloballogentrycounter);
  43. } while (fullWindowsSemaphoreHandle == 0);
  44. }
  45. }
  46. /// <summary>
  47. /// Finalizer.
  48. /// </summary>
  49. ~GlobalLogEntryCounter()
  50. {
  51. Dispose(false);
  52. }
  53. #endregion
  54. #region IDisposable Members
  55. /// <summary>
  56. /// Releases all resources used by this object.
  57. /// </summary>
  58. public void Dispose()
  59. {
  60. Dispose(true);
  61. GC.SuppressFinalize(this);
  62. }
  63. private void Dispose(bool disposing)
  64. {
  65. if (!disposed)
  66. {
  67. disposed = true;
  68. if (disposing)
  69. {
  70. // Dispose managed resources here!
  71. }
  72. if (fullWindowsSemaphoreHandle != 0)
  73. {
  74. CloseHandle(fullWindowsSemaphoreHandle);
  75. fullWindowsSemaphoreHandle = 0;
  76. }
  77. }
  78. }
  79. #endregion
  80. #region Methods
  81. public UInt64 GetNextValue()
  82. {
  83. if (fullWindows)
  84. {
  85. if (fullWindowsSemaphoreHandle != 0)
  86. {
  87. int lastCount;
  88. if (ReleaseSemaphore(fullWindowsSemaphoreHandle, 1, out lastCount))
  89. return (UInt64)lastCount;
  90. }
  91. }
  92. return 0;
  93. }
  94. #endregion
  95. }
  96. }