-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathThreadHelper.cs
More file actions
146 lines (133 loc) · 4.56 KB
/
Copy pathThreadHelper.cs
File metadata and controls
146 lines (133 loc) · 4.56 KB
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
/*
* 作用:多线程处理,在线程中可以实现同步/异步操作。
* */
using System;
using System.Collections.Generic;
using System.Threading;
namespace Helper.Core.Library
{
public class ThreadHelper
{
#region 私有属性常量
// 静态锁对象
private static readonly object lockItem = new object();
// 终止状态时 WaitOne() 允许线程访问下边的语句
private ManualResetEvent manualEvent = new ManualResetEvent(true);
// 已执行结束线程数量
private int callbackCount;
// 线程对象列表
private List<IThreadItem> threadItemList;
// 每个 ThreadItem 执行完回调
private Action<IThreadItem> itemCallback;
// 线程执行结束回调函数
private Action<int> callback;
#endregion
#region 对外公开方法
/// <summary>
/// 启动线程
/// </summary>
/// <param name="threadItemList">线程对象列表</param>
/// <param name="itemCallback">IThreadItem 执行完回调函数</param>
/// <param name="callback">线程结束回调函数</param>
/// <param name="initThreadCount">初始执行线程数量</param>
public void Run(List<IThreadItem> threadItemList, Action<IThreadItem> itemCallback, Action<int> callback, int initThreadCount = 10)
{
this.callbackCount = 0;
this.threadItemList = threadItemList;
this.itemCallback = itemCallback;
this.callback = callback;
ThreadPool.SetMinThreads(1, 1);
ThreadPool.SetMaxThreads(initThreadCount, initThreadCount);
foreach(IThreadItem threadItem in threadItemList)
{
ThreadItemHelper threadItemHelper = new ThreadItemHelper(threadItem, this.ThreadCallback);
System.Threading.WaitCallback waitCallback = new WaitCallback(threadItemHelper.Callback);
ThreadPool.QueueUserWorkItem(waitCallback);
}
this.manualEvent.WaitOne();
this.manualEvent.Reset();
}
/// <summary>
/// 线程等待
/// </summary>
/// <param name="callback">线程等待之后要处理的逻辑</param>
public void Wait(Action callback = null)
{
if (callback != null) callback();
this.manualEvent.WaitOne();
this.manualEvent.Reset();
}
/// <summary>
/// 线程继续
/// </summary>
public void Set()
{
this.manualEvent.Set();
}
#endregion
#region 逻辑处理私有函数
private void ThreadCallback(IThreadItem threadItem)
{
lock (lockItem)
{
if (this.itemCallback != null) this.itemCallback(threadItem);
this.callbackCount++;
if (this.callbackCount >= this.threadItemList.Count && this.callback != null)
{
this.callback(this.callbackCount);
this.callback = null;
this.manualEvent.Set();
}
}
}
#endregion
}
#region 逻辑处理辅助类
/// <summary>
/// 线程帮助类,用来执行线程逻辑
/// </summary>
internal class ThreadItemHelper
{
private IThreadItem threadItem;
private Action<IThreadItem> callback;
public ThreadItemHelper(IThreadItem threadItem, Action<IThreadItem> callback)
{
this.threadItem = threadItem;
this.callback = callback;
}
public void Callback(object obj)
{
if (this.threadItem.IsAsync)
{
this.threadItem.ThreadProcess(() =>
{
if (this.callback != null) this.callback(this.threadItem);
});
}
else
{
this.threadItem.ThreadProcess();
if (this.callback != null) this.callback(this.threadItem);
}
}
}
#endregion
#region 逻辑处理接口
public interface IThreadItem
{
/// <summary>
/// 线程处理逻辑,线程之间异步
/// </summary>
/// <returns></returns>
void ThreadProcess(Action callback);
/// <summary>
/// 线程处理逻辑,线程之间同步
/// </summary>
void ThreadProcess();
/// <summary>
/// 是否异步
/// </summary>
bool IsAsync { get; }
}
#endregion
}