-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #291 from DougSchmidt-AI/feature/PF-1365-NtdnRepor…
…tingTweaks PF-1365 - Minor tweaks
- Loading branch information
Showing
9 changed files
with
167 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
Samples/DotNetSdk/ObservationReportExporter/SingleInstanceGuard.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
using System; | ||
using System.Reflection; | ||
using System.Threading; | ||
using log4net; | ||
|
||
namespace ObservationReportExporter | ||
{ | ||
public class SingleInstanceGuard : IDisposable | ||
{ | ||
// ReSharper disable once PossibleNullReferenceException | ||
private static readonly ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); | ||
|
||
private Mutex InstanceMutex { get; set; } | ||
public string Name { get; } | ||
private bool ShouldRelease { get; set; } | ||
|
||
public SingleInstanceGuard(string name) | ||
{ | ||
Name = $"{ExeHelper.ExeName}.{name}"; | ||
InstanceMutex = new Mutex(true, Name); | ||
ShouldRelease = true; | ||
} | ||
|
||
public bool IsAnotherInstanceRunning() | ||
{ | ||
try | ||
{ | ||
var isAnotherInstanceRunning = !InstanceMutex.WaitOne(TimeSpan.Zero, true); | ||
|
||
if (isAnotherInstanceRunning) | ||
{ | ||
ShouldRelease = false; | ||
} | ||
|
||
return isAnotherInstanceRunning; | ||
} | ||
catch (AbandonedMutexException) | ||
{ | ||
Log.Debug($"Previous run of the program did not clear the '{Name}' mutex cleanly."); | ||
|
||
return false; | ||
} | ||
catch (Exception ex) | ||
{ | ||
Log.Warn($"Error occurred while checking if the program is still running:'{ex.Message}'. Will continue."); | ||
} | ||
|
||
return false; | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
Release(); | ||
|
||
InstanceMutex?.Dispose(); | ||
} | ||
|
||
private void Release() | ||
{ | ||
if (!ShouldRelease) | ||
return; | ||
|
||
try | ||
{ | ||
InstanceMutex?.ReleaseMutex(); | ||
} | ||
catch (Exception e) | ||
{ | ||
Log.Warn($"Can't release mutex '{Name}': {e.Message}"); | ||
} | ||
} | ||
} | ||
} |