-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathThreadHandler.cs
More file actions
130 lines (110 loc) · 3.87 KB
/
ThreadHandler.cs
File metadata and controls
130 lines (110 loc) · 3.87 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
using System.Collections.Generic;
using System.Threading;
namespace iptvChannelChecker
{
/// <summary>
/// Thread wrapper class used for running threads in .NET
/// </summary>
public class ThreadHandler
{
/// <summary>
/// Initializes a new instance of the class
/// </summary>
/// <param name="maximumThreads">The maximum number of threads that can be run at any given time.</param>
public ThreadHandler(int maximumThreads = 4) : this(new List<Thread>(), maximumThreads)
{
}
/// <summary>
/// Initializes a new instance of the class
/// </summary>
/// <param name="threadList">The list of Thread classes that will be run.</param>
/// <param name="maximumThreads">The maximum number of threads that can be run at any given time.</param>
public ThreadHandler(List<Thread> threadList, int maximumThreads = 4)
{
ThreadList = threadList;
MaximumThreads = maximumThreads;
WaitingThreads = new Queue<Thread>();
foreach (var thread in ThreadList) WaitingThreads.Enqueue(thread);
}
/// <summary>
/// The list of Thread classes that will be run.
/// </summary>
public List<Thread> ThreadList { get; }
/// <summary>
/// The maximum number of threads that can be run at any given time.
/// </summary>
public int MaximumThreads { get; }
private Queue<Thread> WaitingThreads { get; }
/// <summary>
/// Checks if any threads are still running.
/// </summary>
public bool IsAlive => ActiveThreads > 0 || WaitingThreads.Count > 0;
public bool KillThreads = false;
/// <summary>
/// Returns the number of currently running threads
/// </summary>
public int ActiveThreads
{
get
{
var count = 0;
lock (ThreadList)
{
foreach (var thread in ThreadList)
if (thread.IsAlive)
count++;
}
return count;
}
}
/// <summary>
/// Runs the threads continuously as they are added to the thread list. The outside process must continuously check
/// the
/// IsAlive property to determine when all the threads have been run.
/// </summary>
public void RunAsynch()
{
while (!KillThreads)
lock (WaitingThreads)
{
if (WaitingThreads.Count != 0 && ActiveThreads < MaximumThreads)
{
var nextThread = WaitingThreads.Dequeue();
nextThread.Start();
}
else
{
Thread.Sleep(1000);
}
}
}
/// <summary>
/// Runs all the threads in the thread list. The method will exit when all threads have been run.
/// </summary>
public void Run()
{
var thread = new Thread(RunAsynch) {IsBackground = true};
thread.Start();
do
{
Thread.Sleep(1000);
} while (IsAlive);
}
/// <summary>
/// Adds a new Thread to the thread list and puts the thread in the waiting threads queue.
/// </summary>
/// <param name="start"></param>
public void Add(ThreadStart start)
{
var thread = new Thread(start);
lock (ThreadList)
{
ThreadList.Add(thread);
}
lock (WaitingThreads)
{
WaitingThreads.Enqueue(thread);
}
}
}
}