forked from cyq1162/cyqdata
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathThreadBreak.cs
170 lines (169 loc) · 5.81 KB
/
ThreadBreak.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Threading;
namespace CYQ.Data.Tool
{
/// <summary>
/// 在ASP.NET中使用多线程时,为了避开升级dll时产生多个线程互相影响,使用此类可以通过外置文件进行跳出。
/// </summary>
public class ThreadBreak
{
bool hadThreadBreak = false;
string threadPath = string.Empty;
/// <summary>
/// 提示:AppConfig中需要配置ThreadBreakPath项的路径
/// </summary>
/// <param name="threadName"></param>
/// <param name="threadID"></param>
public ThreadBreak(string threadName, object threadID)
{
if (ClearThreadBreak(threadName))
{
//创建自身线程
threadPath = AppConfig.WebRootPath + AppConfig.ThreadBreakPath + threadName + "_" + threadID + ".threadbreak";
try
{
File.Create(threadPath).Close();
}
catch (Exception err)
{
Log.WriteLogToTxt(err);
}
hadThreadBreak = true;
}
}
/// <summary>
/// 是否需要退出。
/// </summary>
/// <returns></returns>
public bool NeedToBreak()
{
if (hadThreadBreak)
{
return !File.Exists(threadPath);
}
return false;
}
/// <summary>
/// 清除线程threadbreak文件。
/// </summary>
/// <param name="threadName">线程名称</param>
/// <returns></returns>
private static bool ClearThreadBreak(string threadName)
{
try
{
string threadPath = AppConfig.ThreadBreakPath;
if (!string.IsNullOrEmpty(threadPath))
{
if (threadPath.IndexOf(":\\") == -1)
{
threadPath = AppConfig.WebRootPath + threadPath;
if (!Directory.Exists(threadPath))
{
Directory.CreateDirectory(threadPath);
}
}
//清除其它线程
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>
/// 清除所有的表架构。
/// </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.WriteLogToTxt(err);
}
}
private static List<ParameterizedThreadStart> globalThread = new List<ParameterizedThreadStart>();
private static readonly object lockThreadObj = new object();
/// <summary>
/// 添加全局线程[通常该线程是个死循环,定时处理事情]
/// </summary>
public static void AddGlobalThread(ParameterizedThreadStart start)
{
AddGlobalThread(start, null);
}
public static void AddGlobalThread(ParameterizedThreadStart start, object para)
{
if (globalThread.Count == 0)//第一次加载,清除所有可能存在的线程Break。
{
ClearSchema();
ClearThreadBreak(string.Empty);
}
if (!globalThread.Contains(start))
{
lock (lockThreadObj)
{
try
{
if (!globalThread.Contains(start))
{
globalThread.Add(start);
Thread thread = new Thread(start);
thread.IsBackground = true;
thread.Start(para ?? thread.ManagedThreadId);
}
}
catch (Exception err)
{
Log.WriteLogToTxt(err);
}
}
}
}
}
}