using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace _0526_3
{
// Simple threading scenario: Start a static method running on a second thread
public class ThradExample
{
// The ThreadProc method is called when the thread starts.
// It loops ten times,writing to the console and yielding
// the rest of its time slice each time , and then end.
public static void ThreadProc()
{
for(int i = 0; i < 10; i++)
{
Console.WriteLine("ThreadProc:{0}", i);
// Yield the rest of the time slice.
Thread.Sleep(0);
}
}
public static void Main()
{
Console.WriteLine("Main thread: Start a second thread.");
// The constructor for the Thread class requires a ThreadStart
// delegate the represents the method to be executed on the thread.
// C# simplifies the creating of this delegate.
Thread t = new Thread(new ThreadStart(ThreadProc));
t.Start();
for (int i = 0; i < 4; i++)
{
Console.WriteLine("Main thread: Do some work.");
Thread.Sleep(0);
}
Console.WriteLine("Main thread: Call Join(), to wait until ThreadProc ends.");
t.Join();
Console.WriteLine(
"Main thread: ThreadProc.Join has returned. Press Enter to end program.");
Console.ReadLine();
}
}
}
Threding
最新推荐文章于 2021-03-19 14:34:58 发布