当前位置: 首页>>代码示例>>C#>>正文


C# Thread.Join方法代码示例

本文整理汇总了C#中System.Threading.Thread.Join方法的典型用法代码示例。如果您正苦于以下问题:C# Thread.Join方法的具体用法?C# Thread.Join怎么用?C# Thread.Join使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Threading.Thread的用法示例。


在下文中一共展示了Thread.Join方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Main

//引入命名空间
using System;
using System.Threading;

public class Example
{
   static Thread thread1, thread2;
   
   public static void Main()
   {
      thread1 = new Thread(ThreadProc);
      thread1.Name = "Thread1";
      thread1.Start();
      
      thread2 = new Thread(ThreadProc);
      thread2.Name = "Thread2";
      thread2.Start();   
   }

   private static void ThreadProc()
   {
      Console.WriteLine("\nCurrent thread: {0}", Thread.CurrentThread.Name);
      if (Thread.CurrentThread.Name == "Thread1" && 
          thread2.ThreadState != ThreadState.Unstarted)
         thread2.Join();
      
      Thread.Sleep(4000);
      Console.WriteLine("\nCurrent thread: {0}", Thread.CurrentThread.Name);
      Console.WriteLine("Thread1: {0}", thread1.ThreadState);
      Console.WriteLine("Thread2: {0}\n", thread2.ThreadState);
   }
}
开发者ID:.NET开发者,项目名称:System.Threading,代码行数:32,代码来源:Thread.Join

输出:

Current thread: Thread1

Current thread: Thread2

Current thread: Thread2
Thread1: WaitSleepJoin
Thread2: Running


Current thread: Thread1
Thread1: Running
Thread2: Stopped

示例2: Main

//引入命名空间
using System;
using System.Threading;

public class Example
{
   static Thread thread1, thread2;
   
   public static void Main()
   {
      thread1 = new Thread(ThreadProc);
      thread1.Name = "Thread1";
      thread1.Start();
      
      thread2 = new Thread(ThreadProc);
      thread2.Name = "Thread2";
      thread2.Start();   
   }

   private static void ThreadProc()
   {
      Console.WriteLine("\nCurrent thread: {0}", Thread.CurrentThread.Name);
      if (Thread.CurrentThread.Name == "Thread1" && 
          thread2.ThreadState != ThreadState.Unstarted)
         if (thread2.Join(2000))
            Console.WriteLine("Thread2 has termminated.");
         else
            Console.WriteLine("The timeout has elapsed and Thread1 will resume.");   
      
      Thread.Sleep(4000);
      Console.WriteLine("\nCurrent thread: {0}", Thread.CurrentThread.Name);
      Console.WriteLine("Thread1: {0}", thread1.ThreadState);
      Console.WriteLine("Thread2: {0}\n", thread2.ThreadState);
   }
}
开发者ID:.NET开发者,项目名称:System.Threading,代码行数:35,代码来源:Thread.Join

输出:

Current thread: Thread1

Current thread: Thread2
The timeout has elapsed and Thread1 will resume.

Current thread: Thread2
Thread1: WaitSleepJoin
Thread2: Running


Current thread: Thread1
Thread1: Running
Thread2: Stopped

示例3: TimeSpan

//引入命名空间
using System;
using System.Threading;

class Test
{
    static TimeSpan waitTime = new TimeSpan(0, 0, 1);

    public static void Main() 
    {
        Thread newThread = new Thread(Work);
        newThread.Start();

        if(newThread.Join(waitTime + waitTime)) {
            Console.WriteLine("New thread terminated.");
        }
        else {
            Console.WriteLine("Join timed out.");
        }
    }

    static void Work()
    {
        Thread.Sleep(waitTime);
    }
}
开发者ID:.NET开发者,项目名称:System.Threading,代码行数:26,代码来源:Thread.Join

输出:

New thread terminated.

示例4: Main

//引入命名空间
using System;
using System.Threading;

public class Example
{
   static Thread thread1, thread2;
   
   public static void Main()
   {
      thread1 = new Thread(ThreadProc);
      thread1.Name = "Thread1";
      thread1.Start();
      
      thread2 = new Thread(ThreadProc);
      thread2.Name = "Thread2";
      thread2.Start();   
   }

   private static void ThreadProc()
   {
      Console.WriteLine("\nCurrent thread: {0}", Thread.CurrentThread.Name);
      if (Thread.CurrentThread.Name == "Thread1" && 
          thread2.ThreadState != ThreadState.Unstarted)
         if (thread2.Join(TimeSpan.FromSeconds(2)))
            Console.WriteLine("Thread2 has termminated.");
         else
            Console.WriteLine("The timeout has elapsed and Thread1 will resume.");   
      
      Thread.Sleep(4000);
      Console.WriteLine("\nCurrent thread: {0}", Thread.CurrentThread.Name);
      Console.WriteLine("Thread1: {0}", thread1.ThreadState);
      Console.WriteLine("Thread2: {0}\n", thread2.ThreadState);
   }
}
开发者ID:.NET开发者,项目名称:System.Threading,代码行数:35,代码来源:Thread.Join

输出:

Current thread: Thread1

Current thread: Thread2
The timeout has elapsed and Thread1 will resume.

Current thread: Thread2
Thread1: WaitSleepJoin
Thread2: Running


Current thread: Thread1
Thread1: Running
Thread2: Stopped

示例5: Thread.Join()

//引入命名空间
using System;
using System.Threading;
   
class Sum {
    public Sum(int op1, int op2) {
        Console.WriteLine("[Sum.Sum] Instantiated with values of {0} and {1}", op1, op2);
        this.op1 = op1;
        this.op2 = op2;
    }
    int op1;
    int op2;
    int result;
    public int Result{ get { return result; } }
   
    public void Add()
    {
        Thread.Sleep(5000);
        result = op1 + op2;
    }
}
   
class ThreadData {
    static void Main() {
        Sum sum = new Sum(6, 42);
   
        Thread thread = new Thread(new ThreadStart(sum.Add));
        thread.Start();
   
        for (int i = 0; i < 10; i++) {
            Thread.Sleep(200);
            Console.Write(".");
        }
        thread.Join();
   
        Console.WriteLine("[Main] The result is {0}", sum.Result);
        Console.ReadLine();
    }
}
开发者ID:C#程序员,项目名称:System.Threading,代码行数:39,代码来源:Thread.Join


注:本文中的System.Threading.Thread.Join方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。