本文整理匯總了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);
}
}
輸出:
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);
}
}
輸出:
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);
}
}
輸出:
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);
}
}
輸出:
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();
}
}