Files
MeadeGeneric/Meade.net/GarbageCollection.cs
T
2019-07-19 14:33:41 +01:00

58 lines
1.4 KiB
C#

using System;
using System.Threading;
namespace ASCOM.Meade.net
{
/// <summary>
/// Summary description for GarbageCollection.
/// </summary>
class GarbageCollection
{
private bool _mbContinueThread;
private bool _mGcWatchStopped;
private readonly int _miInterval;
private readonly ManualResetEvent _mEventThreadEnded;
public GarbageCollection(int iInterval)
{
_mbContinueThread = true;
_mGcWatchStopped = false;
_miInterval = iInterval;
_mEventThreadEnded = new ManualResetEvent(false);
}
public void GcWatch()
{
// Pause for a moment to provide a delay to make threads more apparent.
while (ContinueThread())
{
GC.Collect();
Thread.Sleep(_miInterval);
}
_mEventThreadEnded.Set();
}
protected bool ContinueThread()
{
lock (this)
{
return _mbContinueThread;
}
}
public void StopThread()
{
lock (this)
{
_mbContinueThread = false;
}
}
public void WaitForThreadToStop()
{
_mEventThreadEnded.WaitOne();
_mEventThreadEnded.Reset();
}
}
}