ZhiYeJianKang_PeiXun/cyqdata-master/Tool/ThreadBreak.cs

190 lines
6.4 KiB
C#
Raw Normal View History

2025-02-20 15:41:53 +08:00
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Threading;
namespace CYQ.Data.Tool
{
/// <summary>
/// <20><>ASP.NET<45><54>ʹ<EFBFBD>ö<EFBFBD><C3B6>߳<EFBFBD>ʱ<EFBFBD><CAB1>Ϊ<EFBFBD>˱ܿ<CBB1><DCBF><EFBFBD><EFBFBD><EFBFBD>dllʱ<6C><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>̻߳<DFB3><CCBB><EFBFBD>Ӱ<EFBFBD>ʹ<ECA3AC>ô<EFBFBD><C3B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͨ<EFBFBD><CDA8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
/// </summary>
public class ThreadBreak
{
/// <summary>
/// Ӧ<>ó<EFBFBD><C3B3><EFBFBD><EFBFBD>˳<EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD>ɵ<EFBFBD><C9B5>ô˷<C3B4><CBB7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˳<EFBFBD>ȫ<EFBFBD><C8AB><EFBFBD>̡߳<DFB3>whileѭ<65><D1AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
/// </summary>
public static void ClearGlobalThread()
{
if (globalThread.Count > 0)
{
foreach (Thread thread in globalThread)
{
thread.Abort();
}
}
}
bool hadThreadBreak = false;
string threadPath = string.Empty;
/// <summary>
/// <20><>ʾ<EFBFBD><CABE>AppConfig<69><67><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD>ThreadBreakPath<74><68><EFBFBD><EFBFBD>·<EFBFBD><C2B7>
/// </summary>
/// <param name="threadName"></param>
/// <param name="threadID"></param>
public ThreadBreak(string threadName, object threadID)
{
if (ClearThreadBreak(threadName))
{
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>߳<EFBFBD>
threadPath = AppConfig.RunPath + AppConfig.Tool.ThreadBreakPath + threadName + "_" + threadID + ".threadbreak";
try
{
File.Create(threadPath).Close();
}
catch (Exception err)
{
Log.Write(err, LogType.Error);
}
hadThreadBreak = true;
}
}
/// <summary>
/// <20>Ƿ<EFBFBD><C7B7><EFBFBD>Ҫ<EFBFBD>˳<EFBFBD><CBB3><EFBFBD>
/// </summary>
/// <returns></returns>
public bool NeedToBreak()
{
if (hadThreadBreak)
{
return !File.Exists(threadPath);
}
return false;
}
/// <summary>
/// <20><><EFBFBD><EFBFBD><EFBFBD>߳<EFBFBD>threadbreak<61>ļ<EFBFBD><C4BC><EFBFBD>
/// </summary>
/// <param name="threadName"><3E>߳<EFBFBD><DFB3><EFBFBD><EFBFBD><EFBFBD></param>
/// <returns></returns>
private static bool ClearThreadBreak(string threadName)
{
try
{
string threadPath = AppConfig.Tool.ThreadBreakPath;
if (!string.IsNullOrEmpty(threadPath))
{
if (threadPath.IndexOf(":\\") == -1)
{
threadPath = AppConfig.RunPath + threadPath;
if (!Directory.Exists(threadPath))
{
Directory.CreateDirectory(threadPath);
}
}
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>߳<EFBFBD>
DirectoryInfo info = new DirectoryInfo(threadPath);
if (info.Exists)
{
FileInfo[] delInfo = info.GetFiles(threadName + "*.threadbreak");
if (delInfo != null && delInfo.Length > 0)
{
foreach (FileInfo del in delInfo)
{
try
{
if (del.Exists)
{
del.Delete();
}
}
catch
{
continue;
}
}
delInfo = null;
}
return true;
}
}
return false;
}
catch
{
return false;
}
}
/*
/// <summary>
/// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>еı<D0B5><C4B1>ܹ<EFBFBD><DCB9><EFBFBD>
/// </summary>
private static void ClearSchema()
{
try
{
string path = AppConfig.DB.SchemaMapPath;
if (!string.IsNullOrEmpty(path))
{
path = AppConfig.WebRootPath + path;
if (Directory.Exists(path))
{
string[] files = Directory.GetFiles(path, "*.ts");
if (files != null && files.Length > 0)
{
foreach (string file in files)
{
IOHelper.Delete(file);
}
}
}
}
}
catch (Exception err)
{
Log.Write(err, LogType.Error);
}
}
*/
private static List<Thread> globalThread = new List<Thread>();
private static List<string> globalThreadKey = new List<string>();
private static readonly object lockThreadObj = new object();
/// <summary>
/// <20><><EFBFBD><EFBFBD>ȫ<EFBFBD><C8AB><EFBFBD>߳<EFBFBD><><CDA8><EFBFBD><EFBFBD><EFBFBD>߳<EFBFBD><DFB3>Ǹ<EFBFBD><C7B8><EFBFBD>ѭ<EFBFBD><D1AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>]
/// </summary>
public static void AddGlobalThread(ParameterizedThreadStart start)
{
AddGlobalThread(start, null);
}
public static void AddGlobalThread(ParameterizedThreadStart start, object para)
{
if (globalThreadKey.Count == 0)//<2F><>һ<EFBFBD>μ<EFBFBD><CEBC>أ<EFBFBD><D8A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>п<EFBFBD><D0BF>ܴ<EFBFBD><DCB4>ڵ<EFBFBD><DAB5>߳<EFBFBD>Break<61><6B>
{
//ClearSchema();// <20><><EFBFBD><EFBFBD><E1B9B9><EFBFBD>ã<EFBFBD><C3A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD>μ<EFBFBD><CEBC>ص<EFBFBD><D8B5><EFBFBD><EFBFBD><EFBFBD><E2A3AC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E1B9B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD><DAB4>У<EFBFBD><D0A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˲<EFBFBD><CBB2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>~
ClearThreadBreak(string.Empty);
}
string key = Convert.ToString(start.Target) + start.Method.ToString() + Convert.ToString(para);
if (!globalThreadKey.Contains(key))
{
lock (lockThreadObj)
{
try
{
if (!globalThreadKey.Contains(key))
{
globalThreadKey.Add(key);
Thread thread = new Thread(start);
thread.Name = "GlobalThread";
thread.IsBackground = true;
thread.Start(para ?? thread.ManagedThreadId);
globalThread.Add(thread);
}
}
catch (Exception err)
{
Log.Write(err, LogType.Error);
}
}
}
}
}
}