首页 > 代码库 > 使用 Task 替换 ThreadPool ,异步监测所有线程(任务)是否全部执行完毕
使用 Task 替换 ThreadPool ,异步监测所有线程(任务)是否全部执行完毕
判断线程池中线程是否全部执行完成的问题?
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
namespace Ex.RulesTester.Threads
{
/// <summary>
/// 异步等待所有线程结束通知测试
/// </summary>
[TestClass]
public class TaskAllCompleteTester
{
[TestMethod]
public void TestTaskPoolStatus()
{
//运行异步多任务
TaskTest.RunTask();
var list = TaskTest.SuccessList;
bool isAllComplete = false;
//等待异步通知
while (!isAllComplete)
{
isAllComplete = TaskTest.IsAllComplete;
list = TaskTest.SuccessList;
}
Assert.IsTrue(true);
}
/// <summary>
/// 对于 task 多任务执行的封装
/// </summary>
private class TaskTest
{
private static object m_lock = new object();
public static List<string> SuccessList = new List<string>();
private static string msg = "";
public static bool IsFinish = false;
public static List<Task> TaskList = new List<Task>();
static bool isAllComplete = false;
/// <summary>
/// 标记所有任务是否全部执行完毕
/// </summary>
public static bool IsAllComplete
{
get { return isAllComplete; }
}
private TaskTest()
{
}
public static void RunTask()
{
//运行5个任务
for (int i = 1; i <= 5; i++)
{
var task = Task.Factory.StartNew(DoTask, i);
TaskList.Add(task);
}
//异步等待所有任务执行完毕
Task.Factory.StartNew(x =>
{
Task.WaitAll(TaskList.ToArray());
//标记所有任务运行完成
isAllComplete = true;
}, null);
}
private static void DoTask(object par)
{
Thread.Sleep(int.Parse(par.ToString()) * 1000);
lock (m_lock)
{
SuccessList.Add("Has Completed T" + par.ToString());
}
}
}
}
}
草青工作室
using Microsoft.VisualStudio.TestTools.UnitTesting; using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; namespace Ex.RulesTester.Threads { /// <summary> /// 异步等待所有线程结束通知测试 /// </summary> [TestClass] public class TaskAllCompleteTester { [TestMethod] public void TestTaskPoolStatus() { //运行异步多任务 TaskTest.RunTask(); var list = TaskTest.SuccessList; bool isAllComplete = false; //等待异步通知 while (!isAllComplete) { isAllComplete = TaskTest.IsAllComplete; list = TaskTest.SuccessList; } Assert.IsTrue(true); } /// <summary> /// 对于 task 多任务执行的封装 /// </summary> private class TaskTest { private static object m_lock = new object(); public static List<string> SuccessList = new List<string>(); private static string msg = ""; public static bool IsFinish = false; public static List<Task> TaskList = new List<Task>(); static bool isAllComplete = false; /// <summary> /// 标记所有任务是否全部执行完毕 /// </summary> public static bool IsAllComplete { get { return isAllComplete; } } private TaskTest() { } public static void RunTask() { //运行5个任务 for (int i = 1; i <= 5; i++) { var task = Task.Factory.StartNew(DoTask, i); TaskList.Add(task); } //异步等待所有任务执行完毕 Task.Factory.StartNew(x => { Task.WaitAll(TaskList.ToArray()); //标记所有任务运行完成 isAllComplete = true; }, null); } private static void DoTask(object par) { Thread.Sleep(int.Parse(par.ToString()) * 1000); lock (m_lock) { SuccessList.Add("Has Completed T" + par.ToString()); } } } } }
使用 Task 替换 ThreadPool ,异步监测所有线程(任务)是否全部执行完毕
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。