C# 如何取消SmartThreadPool中的当前线程
我正在与C#和库SmartThreadPool合作。C# 如何取消SmartThreadPool中的当前线程,c#,multithreading,threadpool,C#,Multithreading,Threadpool,我正在与C#和库SmartThreadPool合作。 我有两个线程(一个线程被设置为全局变量true/false-调用是t1,一个线程通过智能线程池执行循环-调用是t2) 当t1为false时-t2为工作,当t1为true时-t2为暂停和取消智能线程池创建的所有当前线程正在运行,并将全局变量更新为false。 非常感谢 托米 我的代码: if (STP != null && STP.IsShuttingdown == false) STP.Shutdown(true,
我有两个线程(一个线程被设置为全局变量true/false-调用是t1,一个线程通过智能线程池执行循环-调用是t2)
当t1为false时-t2为工作,当t1为true时-t2为暂停和取消智能线程池创建的所有当前线程正在运行,并将全局变量更新为false。
非常感谢
托米
我的代码:
if (STP != null && STP.IsShuttingdown == false) STP.Shutdown(true, 1000);
STP = new SmartThreadPool(TimeoutSeconds * 10 * 1000, TotalThreads, TotalThreads);
for (int i = 0; i < TotalThreads; i++)
{
CurrentIndex++;
if (CurrentIndex == Total)
{
break;
}
}
Thread thread = new Thread(new ThreadStart(DoWork));
thread.Start();
Thread t2 = new Thread(new ThreadStart(CheckValue));
t2.Start();
thread.Join();
private static void DoWork()
{
while (IsRunning)
{
Thread.Sleep(2000);
if (CurrentIndex >= Total)
{
STP.Shutdown(true, TimeoutSeconds * 10 * 1000);
if (STP.IsShuttingdown)
{
IsRunning = false;
break;
}
}
if (CurrentIndex < Total)
{
for (int i = STP.InUseThreads; i < TotalThreads; i++)
{
if (IsPause)
{
IsPause = false;
}
//do stuff if the server is response (not banned)
Checker sc = new Checker
{
LineIndex = CurrentIndex
};
STP.QueueWorkItem(sc.Checker);
CurrentIndex++;
if (CurrentIndex == Total)
{
break;
}
}
}
}
}
你为什么要这样做?这似乎没有遵循任何关于多线程的最佳实践。您应该包括问题的目的,以便更好地获得相关答案。因为我正在尝试使用蛮力将某个端口连接到服务器,虽然是蛮力,但确实需要检查我的命令是否暂停,如果暂停,它将跳过当前的蛮力端口并继续蛮力。
public static void CheckValue() {
while (true)
{
if (!Helper.CheckWorking())
{
IsPause = true;
}
else
{
IsPause = false;
}
Thread.Sleep(5 * 1000);
}
}