You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
323 lines
9.5 KiB
323 lines
9.5 KiB
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
|
|
{
|
|
|
|
/// <summary>核心方法。</summary>
|
|
public class KernelUtility
|
|
{
|
|
|
|
/// <summary>强制对所有代进行即时垃圾回收。</summary>
|
|
public static void Collect()
|
|
{
|
|
GC.Collect();
|
|
}
|
|
|
|
/// <summary>当前进程为 64 位。</summary>
|
|
public static bool IsInX64
|
|
{
|
|
#if NET20
|
|
get { return IntPtr.Size == 8; }
|
|
#else
|
|
get { return Environment.Is64BitProcess && IntPtr.Size == 8; }
|
|
#endif
|
|
}
|
|
|
|
#region Collect | Dispose
|
|
|
|
/// <summary>开始自动释放内存,默认间隔为 1000 毫秒。</summary>
|
|
public static void StartAutoFree()
|
|
{
|
|
KernelHelper.StartFree(1000);
|
|
}
|
|
|
|
/// <summary>开始自动释放内存,间隔以毫秒为单位。</summary>
|
|
public static void StartAutoFree(int milliseconds)
|
|
{
|
|
KernelHelper.StartFree(milliseconds);
|
|
}
|
|
|
|
/// <summary>停止自动释放内存。</summary>
|
|
public static void StopAutoFree()
|
|
{
|
|
KernelHelper.StopFree();
|
|
}
|
|
|
|
/// <summary>释放。</summary>
|
|
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 { }
|
|
}
|
|
}
|
|
|
|
/// <summary>释放。</summary>
|
|
public static void Dispose<T>(IEnumerable<T> objects, bool flush = false)
|
|
{
|
|
if (objects != null)
|
|
{
|
|
foreach (var i in objects) Dispose(i, flush);
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Thread
|
|
|
|
/// <summary>停止线程。</summary>
|
|
public static void Abort(Thread thread)
|
|
{
|
|
if (thread == null) return;
|
|
try
|
|
{
|
|
if (thread.IsAlive) thread.Abort();
|
|
}
|
|
catch { }
|
|
}
|
|
|
|
/// <summary>阻塞当前线程,时长以毫秒为单位。</summary>
|
|
public static void Sleep(int milliseconds)
|
|
{
|
|
if (milliseconds > 0) Thread.Sleep(milliseconds);
|
|
}
|
|
|
|
/// <summary>在后台线程中执行,指定 Try 将忽略异常。</summary>
|
|
[MethodImpl(MethodImplOptions.NoInlining)]
|
|
public static Thread InBackground(Action action, bool @try = false)
|
|
{
|
|
if (action == null) return null;
|
|
Thread thread;
|
|
if (@try)
|
|
{
|
|
thread = new Thread(delegate (object v)
|
|
{
|
|
try
|
|
{
|
|
((Action)v)();
|
|
}
|
|
catch { }
|
|
});
|
|
}
|
|
else
|
|
{
|
|
thread = new Thread(delegate (object v)
|
|
{
|
|
((Action)v)();
|
|
});
|
|
}
|
|
thread.IsBackground = true;
|
|
thread.Start(action);
|
|
return thread;
|
|
}
|
|
|
|
/// <summary>启动线程。</summary>
|
|
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
|
|
|
|
/// <summary>获取当前进程程序集的资源。</summary>
|
|
public static byte[] GetAssemblyResource(string name)
|
|
{
|
|
var assembly = Assembly.GetExecutingAssembly();
|
|
return GetAssemblyResource(assembly, name);
|
|
}
|
|
|
|
/// <summary>获取指定程序集的资源。</summary>
|
|
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;
|
|
}
|
|
|
|
/// <summary>获取已加载的程序集。</summary>
|
|
public static Assembly[] GetLoadedAssemblies()
|
|
{
|
|
return AppDomain.CurrentDomain.GetAssemblies();
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Cache
|
|
|
|
#if NET461
|
|
|
|
/// <summary>设置缓存项。</summary>
|
|
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;
|
|
}
|
|
|
|
/// <summary></summary>
|
|
public static object CacheGet(string key)
|
|
{
|
|
if (TextUtility.IsEmpty(key)) return null;
|
|
|
|
var instance = MemoryCache.Default;
|
|
var entity = instance.Get(key, null);
|
|
return entity;
|
|
}
|
|
|
|
/// <summary></summary>
|
|
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
|
|
|
|
/// <summary>
|
|
/// <para>当前应用程序所在的目录。</para>
|
|
/// <para>例:D:\App 或 D:\Website</para>
|
|
/// </summary>
|
|
public static string ApplicationPath
|
|
{
|
|
get
|
|
{
|
|
#if NETSTD
|
|
return Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
|
|
#else
|
|
return AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
|
|
#endif
|
|
}
|
|
}
|
|
|
|
#if NETFX || NETCORE
|
|
|
|
/// <summary>处理当前在消息队列中的所有 Windows 消息。</summary>
|
|
public static void DoEvents() => Application.DoEvents();
|
|
|
|
/// <summary>
|
|
/// <para>System.Windows.Forms.Application.StartupPath</para>
|
|
/// <para>c:\windows\system32\inetsrv</para>
|
|
/// </summary>
|
|
public static string StartupPath { get => Application.StartupPath; }
|
|
|
|
/// <summary>
|
|
/// <para>System.Windows.Forms.Application.ExecutablePath</para>
|
|
/// <para>c:\windows\system32\inetsrv\w3wp.exe</para>
|
|
/// </summary>
|
|
public static string ExecutablePath { get => Application.ExecutablePath; }
|
|
|
|
#region System.Windows.Forms.Application
|
|
|
|
/// <summary>在没有窗体的情况下,在当前线程上开始运行标准应用程序消息循环。</summary>
|
|
/// <exception cref="InvalidOperationException"></exception>
|
|
public static void ApplicationRun() => Application.Run();
|
|
|
|
/// <summary>在当前线程上开始运行标准应用程序消息循环,并使指定窗体可见。</summary>
|
|
/// <exception cref="InvalidOperationException"></exception>
|
|
public static void ApplicationRun(Form form) => Application.Run(form);
|
|
|
|
/// <summary>关闭应用程序并立即启动一个新实例。</summary>
|
|
public static void ApplicationRestart() => Application.Restart();
|
|
|
|
/// <summary>通知所有消息泵必须终止,并且在处理了消息以后关闭所有应用程序窗口。</summary>
|
|
public static void ApplicationExit() => Application.Exit();
|
|
|
|
/// <summary>退出当前线程上的消息循环,并关闭该线程上的所有窗口。</summary>
|
|
public static void ApplicationExitThread() => Application.ExitThread();
|
|
|
|
#endregion
|
|
|
|
#endif
|
|
|
|
/// <summary>
|
|
/// <para>System.AppDomain.CurrentDomain.BaseDirectory</para>
|
|
/// <para>D:\Website\</para>
|
|
/// </summary>
|
|
private static string CurrentDomainPath
|
|
{
|
|
get { return AppDomain.CurrentDomain.BaseDirectory; }
|
|
}
|
|
|
|
/// <summary>
|
|
/// <para>System.IO.Directory.GetCurrentDirectory()</para>
|
|
/// <para>c:\windows\system32\inetsrv</para>
|
|
/// </summary>
|
|
private static string CurrentDirectory
|
|
{
|
|
get { return Directory.GetCurrentDirectory(); } // System.Environment.CurrentDirectory
|
|
}
|
|
|
|
/// <summary>
|
|
/// <para>System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName</para>
|
|
/// <para>c:\windows\system32\inetsrv\w3wp.exe</para>
|
|
/// </summary>
|
|
private static string ProcessMainModule
|
|
{
|
|
get { return Process.GetCurrentProcess().MainModule.FileName; }
|
|
}
|
|
|
|
#endregion
|
|
|
|
}
|
|
|
|
}
|
|
|