• 大小: 0.16M
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2024-05-01
  • 语言: C#
  • 标签: await  async  IT  AI  NC  

资源简介


资源截图

代码片段和文件信息

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

namespace asyncAndawait
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(“我是主线程,线程ID:{0}“ Thread.CurrentThread.ManagedThreadId);
            TestAsync();
            Console.ReadLine();
        }

         static async Task TestAsync()
        {
            Console.WriteLine(“调用GetReturnResult()之前,线程ID:{0}。当前时间:{1}“ Thread.CurrentThread.ManagedThreadId DateTime.Now.ToString(“yyyy-MM-dd hh:MM:ss“));
            var name = GetReturnResult();
            //var ss=name.GetAwaiter();
            Console.WriteLine(“调用GetReturnResult()之后,线程ID:{0}。当前时间:{1}“ Thread.CurrentThread.ManagedThreadId DateTime.Now.ToString(“yyyy-MM-dd hh:MM:ss“));
            Console.WriteLine(“此时的 name 的类型是 {0}{1}“ name DateTime.Now.ToString());
            Console.WriteLine(“下面准备调用 await name“);
            Console.WriteLine(“得到GetReturnResult()方法的结果:{0}。当前时间:{1}“await  name DateTime.Now.ToString(“yyyy-MM-dd hh:MM:ss“));
        }

        static async Task GetReturnResult()
        {
            Console.WriteLine(“执行Task.Run之前 线程ID:{0}“ Thread.CurrentThread.ManagedThreadId);
            return await Task.Run(() =>
            {
                Thread.Sleep(3000);
                Console.WriteLine(“GetReturnResult()方法里面线程ID: {0}“ Thread.CurrentThread.ManagedThreadId);
                return “我是返回值“;
            });
        }
    }
}

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2020-10-16 11:22  asyncAndawait2\
     目录           0  2020-10-16 11:22  asyncAndawait2\asyncAndawait\
     目录           0  2018-08-06 14:12  asyncAndawait2\asyncAndawait\.vs\
     目录           0  2018-08-06 14:12  asyncAndawait2\asyncAndawait\.vs\asyncAndawait\
     目录           0  2018-08-06 14:12  asyncAndawait2\asyncAndawait\.vs\asyncAndawait\v15\
     文件       46592  2018-08-30 19:24  asyncAndawait2\asyncAndawait\.vs\asyncAndawait\v15\.suo
     目录           0  2018-08-06 14:12  asyncAndawait2\asyncAndawait\.vs\asyncAndawait\v15\Server\
     目录           0  2018-08-22 14:26  asyncAndawait2\asyncAndawait\.vs\asyncAndawait\v15\Server\sqlite3\
     文件           0  2018-08-06 14:12  asyncAndawait2\asyncAndawait\.vs\asyncAndawait\v15\Server\sqlite3\db.lock
     文件      450560  2018-08-22 14:26  asyncAndawait2\asyncAndawait\.vs\asyncAndawait\v15\Server\sqlite3\storage.ide
     目录           0  2018-08-21 20:01  asyncAndawait2\asyncAndawait\asyncAndawait\
     文件         167  2020-10-16 11:22  asyncAndawait2\asyncAndawait\asyncAndawait\App.config
     文件        1751  2018-08-21 20:01  asyncAndawait2\asyncAndawait\asyncAndawait\Program.cs
     目录           0  2018-08-06 14:12  asyncAndawait2\asyncAndawait\asyncAndawait\Properties\
     文件        1324  2018-08-06 14:12  asyncAndawait2\asyncAndawait\asyncAndawait\Properties\AssemblyInfo.cs
     文件        2365  2020-10-16 11:22  asyncAndawait2\asyncAndawait\asyncAndawait\asyncAndawait.csproj
     目录           0  2018-08-06 14:12  asyncAndawait2\asyncAndawait\asyncAndawait\bin\
     目录           0  2020-10-16 11:22  asyncAndawait2\asyncAndawait\asyncAndawait\bin\Debug\
     文件        8192  2020-10-16 11:23  asyncAndawait2\asyncAndawait\asyncAndawait\bin\Debug\asyncAndawait.exe
     文件         167  2020-10-16 11:22  asyncAndawait2\asyncAndawait\asyncAndawait\bin\Debug\asyncAndawait.exe.config
     文件       17920  2020-10-16 11:23  asyncAndawait2\asyncAndawait\asyncAndawait\bin\Debug\asyncAndawait.pdb
     文件       22984  2020-10-16 11:22  asyncAndawait2\asyncAndawait\asyncAndawait\bin\Debug\asyncAndawait.vshost.exe
     文件         167  2020-10-16 11:22  asyncAndawait2\asyncAndawait\asyncAndawait\bin\Debug\asyncAndawait.vshost.exe.config
     文件         490  2010-03-17 22:39  asyncAndawait2\asyncAndawait\asyncAndawait\bin\Debug\asyncAndawait.vshost.exe.manifest
     目录           0  2018-08-06 14:12  asyncAndawait2\asyncAndawait\asyncAndawait\obj\
     目录           0  2020-10-16 11:23  asyncAndawait2\asyncAndawait\asyncAndawait\obj\Debug\
     文件        6706  2020-10-16 11:22  asyncAndawait2\asyncAndawait\asyncAndawait\obj\Debug\DesignTimeResolveAssemblyReferencesInput.cache
     目录           0  2020-10-16 13:07  asyncAndawait2\asyncAndawait\asyncAndawait\obj\Debug\TempPE\
     文件           0  2018-08-06 14:12  asyncAndawait2\asyncAndawait\asyncAndawait\obj\Debug\TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs
     文件           0  2018-08-06 14:12  asyncAndawait2\asyncAndawait\asyncAndawait\obj\Debug\TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs
     文件           0  2018-08-06 14:12  asyncAndawait2\asyncAndawait\asyncAndawait\obj\Debug\TemporaryGeneratedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs
............此处省略7个文件信息

评论

共有 条评论