• 大小: 0.02M
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2024-04-29
  • 语言: C#
  • 标签: TASK  tEST  

资源简介

Task使用实例

资源截图

代码片段和文件信息

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace TaskTest
{
    class Program
    {
        static void Main(string[] args)
        {
            #region Task的状态
            //var task1 = new Task(() =>
            //{
            //    Console.WriteLine(“Begin“);
            //    System.Threading.Thread.Sleep(2000);
            //    Console.WriteLine(“Finish“);
            //});
            //Console.WriteLine(“Before start:“ + task1.Status);
            //task1.Start();
            //Console.WriteLine(“After start:“ + task1.Status);
            //task1.Wait();
            //Console.WriteLine(“After Finish:“ + task1.Status);

            //Console.Read();
            #endregion

            #region 多任务执行
            //var task1 = new Task(() =>
            //{
            //    Console.WriteLine(“Task 1 Begin“);
            //    System.Threading.Thread.Sleep(2000);
            //    Console.WriteLine(“Task 1 Finish“);
            //});
            //var task2 = new Task(() =>
            //{
            //    Console.WriteLine(“Task 2 Begin“);
            //    System.Threading.Thread.Sleep(3000);
            //    Console.WriteLine(“Task 2 Finish“);
            //});

            //task1.Start();
            //task2.Start();
            //Task.WaitAll(task1 task2);
            //Console.WriteLine(“All task finished!“);

            //Console.Read();
            #endregion

            #region 等待任一个任务执行完毕
            //var task1 = new Task(() =>
            //{
            //    Console.WriteLine(“Task 1 Begin“);
            //    System.Threading.Thread.Sleep(2000);
            //    Console.WriteLine(“Task 1 Finish“);
            //});
            //var task2 = new Task(() =>
            //{
            //    Console.WriteLine(“Task 2 Begin“);
            //    System.Threading.Thread.Sleep(3000);
            //    Console.WriteLine(“Task 2 Finish“);
            //});

            //task1.Start();
            //task2.Start();
            //Task.WaitAny(task1 task2);
            //Console.WriteLine(“All task finished!“);

            //Console.Read();
            #endregion

            #region 一个Task执行完成之后执行另一个Task
            //var task1 = new Task(() =>
            //{
            //    Console.WriteLine(“Task 1 Begin“);
            //    System.Threading.Thread.Sleep(2000);
            //    Console.WriteLine(“Task 1 Finish“);
            //});
            //var task2 = new Task(() =>
            //{
            //    Console.WriteLine(“Task 2 Begin“);
            //    System.Threading.Thread.Sleep(3000);
            //    Console.WriteLine(“Task 2 Finish“);
            //});

            //task1.Start();
            //task2.Start();
            //var result = task1.ContinueWith(task =>
            //{
            //    Console.WriteLine

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2020-07-22 17:31  TaskTest\
     目录           0  2020-07-22 17:07  TaskTest\.vs\
     目录           0  2020-07-22 17:07  TaskTest\.vs\TaskTest\
     目录           0  2020-07-22 17:07  TaskTest\.vs\TaskTest\v16\
     文件       20992  2020-07-22 17:33  TaskTest\.vs\TaskTest\v16\.suo
     文件        5111  2020-07-22 17:31  TaskTest\Program.cs
     目录           0  2020-07-22 17:06  TaskTest\Properties\
     文件        1308  2020-07-22 17:06  TaskTest\Properties\AssemblyInfo.cs
     文件        2182  2020-07-22 17:06  TaskTest\TaskTest.csproj
     文件        1121  2020-07-22 17:07  TaskTest\TaskTest.sln
     目录           0  2020-07-22 17:06  TaskTest\bin\
     目录           0  2020-07-22 17:08  TaskTest\bin\Debug\
     文件        5632  2020-07-22 17:31  TaskTest\bin\Debug\TaskTest.exe
     文件       22016  2020-07-22 17:31  TaskTest\bin\Debug\TaskTest.pdb
     目录           0  2020-07-22 17:06  TaskTest\obj\
     目录           0  2020-07-22 17:31  TaskTest\obj\Debug\
     文件         208  2020-07-22 17:06  TaskTest\obj\Debug\.NETframeworkVersion=v4.0.AssemblyAttributes.cs
     文件        6449  2020-07-22 17:06  TaskTest\obj\Debug\DesignTimeResolveAssemblyReferencesInput.cache
     文件          42  2020-07-22 17:07  TaskTest\obj\Debug\TaskTest.csproj.CoreCompileInputs.cache
     文件         443  2020-07-22 17:08  TaskTest\obj\Debug\TaskTest.csproj.FileListAbsolute.txt
     文件         424  2020-07-22 17:31  TaskTest\obj\Debug\TaskTest.csprojAssemblyReference.cache
     文件        5632  2020-07-22 17:31  TaskTest\obj\Debug\TaskTest.exe
     文件       22016  2020-07-22 17:31  TaskTest\obj\Debug\TaskTest.pdb
     目录           0  2020-09-21 12:06  TaskTest\obj\Debug\TempPE\

评论

共有 条评论