using Apewer.Internals;
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Threading;
using System.IO;
using System.Diagnostics;
#if NET461
using System.Runtime.Caching;
#endif
#if NETFX || NETCORE
using System.Windows.Forms;
#endif
namespace Apewer
{
/// 核心方法。
public class KernelUtility
{
/// 强制对所有代进行即时垃圾回收。
public static void Collect()
{
GC.Collect();
}
/// 当前进程为 64 位。
public static bool IsInX64
{
#if NET20
get { return IntPtr.Size == 8; }
#else
get { return Environment.Is64BitProcess && IntPtr.Size == 8; }
#endif
}
#region Collect | Dispose
/// 开始自动释放内存,默认间隔为 1000 毫秒。
public static void StartAutoFree()
{
KernelHelper.StartFree(1000);
}
/// 开始自动释放内存,间隔以毫秒为单位。
public static void StartAutoFree(int milliseconds)
{
KernelHelper.StartFree(milliseconds);
}
/// 停止自动释放内存。
public static void StopAutoFree()
{
KernelHelper.StopFree();
}
/// 释放。
public static void Dispose(object target, bool flush = false)
{
if (target == null) return;
var stream = target as Stream;
if (stream != null)
{
if (flush)
{
try { stream.Flush(); } catch { }
}
try { stream.Close(); } catch { }
}
var disposable = target as IDisposable;
if (disposable != null)
{
try
{
disposable.Dispose();
}
catch { }
}
}
/// 释放。
public static void Dispose(IEnumerable objects, bool flush = false)
{
if (objects != null)
{
foreach (var i in objects) Dispose(i, flush);
}
}
#endregion
#region Thread
/// 停止线程。
public static void Abort(Thread thread)
{
if (thread == null) return;
try
{
if (thread.IsAlive) thread.Abort();
}
catch { }
}
/// 阻塞当前线程,时长以毫秒为单位。
public static void Sleep(int milliseconds)
{
if (milliseconds > 0) Thread.Sleep(milliseconds);
}
///
[MethodImpl(MethodImplOptions.NoInlining)]
public static Thread InBackground(Action action)
{
if (action == null) return null;
var thread = new Thread(delegate (object v) { ((Action)v)(); });
thread.IsBackground = true;
thread.Start(action);
return thread;
}
/// 启动线程。
public static Thread StartThread(Action action, bool background = true)
{
if (action == null) return null;
var thread = new Thread(new ThreadStart(action));
thread.IsBackground = background;
thread.Start();
return thread;
}
#endregion
#region Assembly
/// 获取当前进程程序集的资源。
public static byte[] GetAssemblyResource(string name)
{
var assembly = Assembly.GetExecutingAssembly();
return GetAssemblyResource(assembly, name);
}
/// 获取指定程序集的资源。
public static byte[] GetAssemblyResource(Assembly assembly, string name)
{
var result = Constant.EmptyBytes;
if (assembly == null) return null;
try
{
var source = assembly.GetManifestResourceStream(name);
result = StreamHelper.Read(source);
Dispose(source);
}
finally { }
return result;
}
/// 获取已加载的程序集。
public static Assembly[] GetLoadedAssemblies()
{
return AppDomain.CurrentDomain.GetAssemblies();
}
#endregion
#region Cache
#if NET461
/// 设置缓存项。
public static bool CacheSet(string key, object value, int minutes = 60)
{
if (TextUtility.IsEmpty(key)) return false;
if (minutes < 1) return false;
var policy = new CacheItemPolicy();
policy.AbsoluteExpiration = DateTimeOffset.Now.AddMinutes((double)NumberUtility.RestrictValue(minutes, 0, 525600));
var instance = MemoryCache.Default;
instance.Set(key, value, policy, null);
return true;
}
///
public static object CacheGet(string key)
{
if (TextUtility.IsEmpty(key)) return null;
var instance = MemoryCache.Default;
var entity = instance.Get(key, null);
return entity;
}
///
public static object CacheRemove(string key)
{
if (TextUtility.IsEmpty(key)) return null;
var instance = MemoryCache.Default;
var entity = instance.Remove(key, null);
return entity;
}
#endif
#endregion
#region Application
#if NETFX || NETCORE
///
/// System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase
/// D:\Website\
///
public static string ApplicationBasePath {get=> AppDomain.CurrentDomain.SetupInformation.ApplicationBase; }
/// 处理当前在消息队列中的所有 Windows 消息。
public static void DoEvents() => Application.DoEvents();
///
/// System.Windows.Forms.Application.StartupPath
/// c:\windows\system32\inetsrv
///
public static string StartupPath { get => Application.StartupPath; }
///
/// System.Windows.Forms.Application.ExecutablePath
/// c:\windows\system32\inetsrv\w3wp.exe
///
public static string ExecutablePath { get => Application.ExecutablePath; }
#region System.Windows.Forms.Application
/// 在没有窗体的情况下,在当前线程上开始运行标准应用程序消息循环。
///
public static void ApplicationRun() => Application.Run();
/// 在当前线程上开始运行标准应用程序消息循环,并使指定窗体可见。
///
public static void ApplicationRun(Form form) => Application.Run(form);
/// 关闭应用程序并立即启动一个新实例。
public static void ApplicationRestart() => Application.Restart();
/// 通知所有消息泵必须终止,并且在处理了消息以后关闭所有应用程序窗口。
public static void ApplicationExit() => Application.Exit();
/// 退出当前线程上的消息循环,并关闭该线程上的所有窗口。
public static void ApplicationExitThread() => Application.ExitThread();
#endregion
#endif
///
/// System.AppDomain.CurrentDomain.BaseDirectory
/// D:\Website\
///
private static string CurrentDomainPath
{
get { return AppDomain.CurrentDomain.BaseDirectory; }
}
///
/// System.IO.Directory.GetCurrentDirectory()
/// c:\windows\system32\inetsrv
///
private static string CurrentDirectory
{
get { return Directory.GetCurrentDirectory(); } // System.Environment.CurrentDirectory
}
///
/// System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName
/// c:\windows\system32\inetsrv\w3wp.exe
///
private static string ProcessMainModule
{
get { return Process.GetCurrentProcess().MainModule.FileName; }
}
#endregion
}
}