[C#] 纯文本查看 复制代码 /// <summary>
/// 真实执行下载的线程
/// </summary>
/// <param name="objPath"></param>
private static void DownLoadPicThread(object objPath)
{
//模拟下载图片
Console.WriteLine($"下载图片:{objPath.ToString()}");
Thread.Sleep(3000);
}
/// <summary>
/// 独立线程,用于启动子线程
/// </summary>
private static void DownloadMain()
{
List<Task> tasks = new List<Task>();
for (int i = 0; i < 30; i++)
{
Task task = new Task(DownLoadPicThread, i, TaskCreationOptions.LongRunning);
task.Start();
tasks.Add(task);
}
Task.WaitAll(tasks.ToArray());
Console.WriteLine("所有线程都已执行完成");
}
static void Main(string[] args)
{
Task taskMain = new Task(DownloadMain);
taskMain.Start();
Console.ReadKey();
} |