ソース

http://www.atmarkit.co.jp/fdotnet/mthread/mthread02/mthread02_02.html


Threadクラス


using System;

using System.Threading;

public class List1_1
{
public static void Main()
{
Thread threadA = new Thread(new ThreadStart(ThreadMethod)); //

threadA.Start(); //

for (int i = 0; i < 100; i++)
{
Thread.Sleep(5);
Console.Write(" B ");
}
}

// 別スレッドで動作させるメソッド
private static void ThreadMethod()
{
for (int i = 0; i < 100; i++)
{
Thread.Sleep(5);
Console.Write(" A ");
}
}

}
------------------------------------------------------------------------------



using System;
using System.Threading;

public class List1_2
{
public static void Main()
{
ThreadedTextPrinter printer = new ThreadedTextPrinter(); //
printer.Print("AA"); //

for (int i = 0; i < 100; i++)
{
Thread.Sleep(5);
Console.Write(" B ");
}
}
}

public class ThreadedTextPrinter //
{
private string text;

public void Print(string s) //
{
text = s; //

Thread thread = new Thread(new ThreadStart(ThreadMethod));

thread.Priority = ThreadPriority.Highest;
thread.Start();
}

// 別スレッドで動作させるメソッド
private void ThreadMethod() //
{
for (int i = 0; i < 100; i++)
{
Thread.Sleep(5);
Console.Write(" {0} ", text); //
}
}
}

-------------------------------------------------------------------


ThreadPoolクラス



using System;
using System.Threading;

public class List2
{
public static void Main()
{
WaitCallback waitCB = new WaitCallback(ThreadMethod);

ThreadPool.QueueUserWorkItem(waitCB, "a");
ThreadPool.QueueUserWorkItem(waitCB, "b");
Console.ReadLine();
}

private static void ThreadMethod(object state)
{
for (int i=0; i < 100; i++)
{
Thread.Sleep(5);
Console.Write(state+" ");
}
}

-----------------------------------------------------------------

Timer


using System;
using System.Threading;

public class List4
{
public static void Main()
{
TimerCallback tCB = new TimerCallback(ThreadMethod);
Console.WriteLine(DateTime.Now);
Timer t = new Timer(tCB, null, 2000, 1000);
Console.ReadLine();

}

public static void ThreadMethod(object state)
{
Console.WriteLine(DateTime.Now);
}
}