csharp csharp_test_taskcompletionsource_and_threads.cs

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp csharp_test_taskcompletionsource_and_threads.cs相关的知识,希望对你有一定的参考价值。

using System;
using System.Threading.Tasks;
using System.Threading;

public class MainClass {
  
  static TaskCompletionSource<string> globaltask;
  public static void Main (string[] args) {
    var task = new TaskCompletionSource<string> ();
    globaltask = task;
   Task.Run(() => a1(task));
   Task.Run(() => a2(task));
   Task.Run(() => a3(task));
  // new Thread( () => a1(task)).Start();
  // new Thread( () => a2(task)).Start();
  // new Thread( () => a3(task)).Start();
  //a1(task);
  //a2(task);
  //a3(task);

  Console.WriteLine(System.Threading.Thread.CurrentThread.ManagedThreadId);
  Console.WriteLine("before controller");

  Task.Run( () => controller(ref task));
  Console.WriteLine("after controller");
    task.SetResult("done");
  Console.WriteLine(task==null);
  Console.WriteLine("after done");
  task = null;
  Console.WriteLine("END.");
  }
  
  public static async void a1(TaskCompletionSource<string> task)
  {
    // Console.WriteLine("before await: {0}", System.Threading.Thread.CurrentThread.ManagedThreadId);
    await task.Task;
     Console.WriteLine("after await: {0}", System.Threading.Thread.CurrentThread.ManagedThreadId);
    System.Threading.Thread.Sleep(3000);
    Console.Write("a1 ");
    Console.WriteLine(task.Task.Result);
    if (task == globaltask)      Console.WriteLine("equal!");
    if (task == null)
      Console.WriteLine(" task is null at {0}", "a1");
  }
    public static async void a2(TaskCompletionSource<string> task)
  {
    // Console.WriteLine("before await: {0}", System.Threading.Thread.CurrentThread.ManagedThreadId);
    await task.Task;
     Console.WriteLine("after await: {0}", System.Threading.Thread.CurrentThread.ManagedThreadId);
    System.Threading.Thread.Sleep(2000);
    Console.Write("a2 ");
    Console.WriteLine(task.Task.Result);
    if (task == null)
      Console.WriteLine(" task is null at {0}", "a2");
  }
    public static async void a3(TaskCompletionSource<string> task)
  {
    // Console.WriteLine("before await: {0}", System.Threading.Thread.CurrentThread.ManagedThreadId);
    await task.Task;
     Console.WriteLine("after await: {0}", System.Threading.Thread.CurrentThread.ManagedThreadId);
    System.Threading.Thread.Sleep(1);
    Console.Write("a3 ");
    Console.WriteLine(task.Task.Result);
    if (task == null)
      Console.WriteLine(" task is null at {0}", "a3");
  }

  public static void controller(ref TaskCompletionSource<string> task)
  {
    Console.WriteLine("controller start");
    //Thread.Sleep(10);
    task = null;
    if (task == globaltask)      Console.WriteLine("equal!");
    Console.WriteLine("controller end. Task is null");

  }
}

以上是关于csharp csharp_test_taskcompletionsource_and_threads.cs的主要内容,如果未能解决你的问题,请参考以下文章

csharp 例如-CSHARP-GroupDocs.Conversion.Examples.CSharp渲染,RenderPSDImageAsPath.cs

csharp 实例-CSHARP-GroupDocs.Conversion.Examples.CSharp变频-ConvertTopresentationAsPath.cs

csharp 实例-CSHARP-GroupDocs.Conversion.Examples.CSharp变频-ConvertTopresentationAsPath.cs

csharp 实例-CSHARP-GroupDocs.Conversion.Examples.CSharp变频-ConvertTopresentationAsPath.cs

csharp 例如-CSHARP-GroupDocs.Search.Examples.CSharp索引,AddDocumentToIndex.cs

csharp 例如-CSHARP-GroupDocs.Search.Examples.CSharp索引,AddDocumentToIndexAsynchronously.cs