C#缓存线程模板

  • w4_915702
    了解作者
  • C#
    开发工具
  • 2KB
    文件大小
  • zip
    文件格式
  • 0
    收藏次数
  • VIP专享
    资源类型
  • 0
    下载次数
  • 2022-02-18 10:16
    上传日期
缓存线程基类,使用很方便,在需要缓存的开发项目中可以复用
SoftCode.zip
  • CommonEventThread.txt
    1KB
  • CommonThread.txt
    3.2KB
  • CacheThread.txt
    1.8KB
内容介绍
namespace HongRui.Platform.Common { using System; using System.Runtime.CompilerServices; using System.Threading; public abstract class CommonThread { private ManualResetEvent ExitEndEvent; protected ManualResetEvent ExitEvent; protected int millisecondsTimeout; protected Thread thread; public CommonThread() { this.ExitEvent = new ManualResetEvent(false); this.ExitEndEvent = new ManualResetEvent(false); this.millisecondsTimeout = 0xea60; } public CommonThread(int millisecondsTimeout) { this.ExitEvent = new ManualResetEvent(false); this.ExitEndEvent = new ManualResetEvent(false); this.millisecondsTimeout = 0xea60; this.millisecondsTimeout = millisecondsTimeout; } public void End() { this.WaitAndEnd(-1); } public void ForceEnd() { this.WaitAndEnd(this.millisecondsTimeout); } public void ForceEnd(int delayTime) { this.WaitAndEnd(delayTime); } protected virtual bool Init() => true; public bool IsExit() { WaitHandle[] waitHandles = new WaitHandle[] { this.ExitEvent }; return WaitHandle.WaitAll(waitHandles, 0, true); } protected void Loop() { if (this.Init()) { this.MainLoop(); } else { CustomLog.Warn("Thread Init Failed"); } this.UnLoad(); this.ExitEndEvent.Set(); } protected virtual void MainLoop() { WaitHandle[] handleArray; do { handleArray = new WaitHandle[] { this.ExitEvent }; } while (!WaitHandle.WaitAll(handleArray, this.millisecondsTimeout, true) && !this.MainLoopFun()); } protected virtual bool MainLoopFun() => true; public void Start() { if (this.thread == null) { ThreadStart start = new ThreadStart(this.Loop); this.thread = new Thread(start); this.thread.Start(); } } protected virtual void UnLoad() { } private void WaitAndEnd(int waitMillisecondsTimeout) { if (this.thread != null) { this.ExitEvent.Set(); WaitHandle[] waitHandles = new WaitHandle[] { this.ExitEndEvent }; if (WaitHandle.WaitAll(waitHandles, waitMillisecondsTimeout, true)) { CustomLog.Warn("Thread Exit Successed!"); } else { CustomLog.Warn("Thread Exit Failed!"); this.thread.Abort(""); } this.ExitEvent.Reset(); this.ExitEndEvent.Reset(); this.thread = null; } } public string ThreadName { get; set; } } }
评论
    相关推荐
    • c# SmartGridView
      c# SmartGridView c# SmartGridView
    • c#点歌系统
      c#点歌系统c#点歌系统c#点歌系统c#点歌系统c#点歌系统c#点歌系统c#点歌系统c#点歌系统c#点歌系统c#点歌系统c#点歌系统c#点歌系统c#点歌系统c#点歌系统c#点歌系统c#点歌系统c#点歌系统c#点歌系统c#点歌系统c#点歌系统...
    • C#記事本
      C#記事本源代碼 C#記事本源代碼 C#記事本源代碼
    • C# 4.0
      C# 4.0揭秘C# 4.0揭秘C# 4.0揭秘C# 4.0揭秘C# 4.0揭秘C# 4.0揭秘C# 4.0揭秘C# 4.0揭秘C# 4.0揭秘
    • C#时钟
      C#时钟C#时钟C#时钟C#时钟C#时钟C#时钟C#时钟
    • Effective C#
      Effective C#. Effective C#.
    • c# splashsreen
      c# splashsreen c# splashsreen c# splashsreen c# splashsreen c# splashsreen c# splashsreen
    • C#Languageexamples
      CCCCCCCC#的入门学习例子,包括了C#gefangmiande yufa 有助于在短时间内学好C#
    • c# splashsreen
      c# splashsreen c# splashsreen c# splashsreen c# splashsreen c# splashsreen
    • C# 教程
      C#教程》以Microsoft Visual Studio 2008作为操作平台,包含教程、练习、实验部分。教程部分首先熟悉开发环境,然后比较系统地介绍C#语言基础、C#面向对象编程基础和C#面向对象编程进阶;在此基础上系统介绍...