美文网首页
2024-01-15

2024-01-15

作者: 午字横 | 来源:发表于2024-01-14 14:56 被阅读0次

    Run(Action)

    将在线程池上运行的指定工作排队,并返回代表该工作的 Task 对象。

    C#复制

    public static System.Threading.Tasks.Task Run (Action action);
    
    using System;
    using System.Threading;
    using System.Threading.Tasks;
    
    public class Example
    {
       public static void Main()
       {
          ShowThreadInfo("Application");
    
          var t = Task.Run(() => ShowThreadInfo("Task") );
          t.Wait();
       }
    
       static void ShowThreadInfo(String s)
       {
          Console.WriteLine("{0} thread ID: {1}",
                            s, Thread.CurrentThread.ManagedThreadId);
       }
    }
    // The example displays the following output:
    //       Application thread ID: 1
    //       Task thread ID: 3
    

    以异步方式执行的工作。

    using System;
    using System.Threading;
    using System.Threading.Tasks;
    
    public class Example
    {
       public static void Main()
       {
          Console.WriteLine("Application thread ID: {0}",
                            Thread.CurrentThread.ManagedThreadId);
          var t = Task.Run(() => {  Console.WriteLine("Task thread ID: {0}",
                                       Thread.CurrentThread.ManagedThreadId);
                                 } );
          t.Wait();
       }
    }
    // The example displays the following output:
    //       Application thread ID: 1
    //       Task thread ID: 3
    

    2024-01-15

    相关文章

      网友评论

          本文标题:2024-01-15

          本文链接:https://www.haomeiwen.com/subject/khqcodtx.html