using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading;

namespace Apewer.Internals
{

    internal class KernelHelper
    {

        private static Thread _freethread = null;
        private static int _freesleep = 1000;

        private static void FreeListen()
        {
            while (true)
            {
                GC.Collect();
                Thread.Sleep(_freesleep);
            }
        }

        /// <summary>开始自动释放内存,间隔以毫秒为单位。</summary>
        public static void StartFree(int argInterval = 1000)
        {
            StopFree();
            _freesleep = argInterval > 0 ? argInterval : 1000;
            _freethread = new Thread(FreeListen);
            _freethread.IsBackground = true;
            _freethread.Start();
        }

        /// <summary>停止自动释放内存。</summary>
        public static bool StopFree()
        {
            if (_freethread == null) return true;
            try
            {
                if (_freethread.IsAlive) _freethread.Abort();
                _freethread = null;
                return true;
            }
            catch
            {
                return false;
            }
        }      
        
    }

}