【C#】スレッド・プログラム ~入門編~

■ スレッドに関する用語

 * 以下の関連記事を参照のこと。
https://blogs.yahoo.co.jp/dk521123/37076571.html

■ Threadクラス

メソッド

Join()
 * スレッドの終了を待ち合わせる

メンバ変数

ThreadState 列挙体
 * スレッドの状態を調べる
http://msdn.microsoft.com/ja-jp/library/system.threading.threadstate.aspx

■ サンプル

例1

Program.cs
using System;
using System.Threading;

namespace ThreadDemo
{
    class ThreadDemo1
    {
        Thread child;

        public ThreadDemo1()
        {
            child = new Thread(ChildThread);	// MAKE An Object of Child Thread
            child.Start();	// START a Thread
        }

        static void Main(string[] args)
        {
            ThreadDemo1 parent = new ThreadDemo1();
            
            // To Display for showing us the status for Main thread
            Console.WriteLine("Main Thread starts.");
            Console.WriteLine("Main Thread sleeps for 10 sec!!");
            
            try {
                // The Main Threas stop for 10 sec.
                Thread.Sleep(10000); // Go to sleep. Zzz...
            } catch(Exception e) {
                Console.WriteLine("Error:" + e);
            }
            // To Display for showing us the status for Main thread
            Console.WriteLine("Main Thread works again.");
            Console.WriteLine("Main Thread stops working.");

            Console.Read();
        }

        public void ChildThread()
        {
            // To Display for showing us the status for Child thread
            Console.WriteLine("Child Thread starts.");
            Console.WriteLine("Child Thread sleeps for 5 sec!");
            
            try {
                // The Child Threas stop for 5sec.
                Thread.Sleep(5000);	// Go to sleep. Zzz...
            } catch(Exception e) {
                Console.WriteLine("Error:" + e);
            }
            // To Display for showing us the status for Child thread
            Console.WriteLine("Child Thread works again.");
            Console.WriteLine("Child Thread stops working.");
        }
    }
}

例2

Form1.cs
private void button1_Click(object sender, EventArgs e)
{
    //スレッドクラスの作成
    SampleThread sampleThread = new SampleThread();
    Thread thread = new Thread(new ThreadStart(sampleThread.SampleMethod));

    // スレッド実行
    thread.Start();
    // manualResetEventがシグナル状態になるのを待ちます
    sampleThread.manualResetEvent.WaitOne();

    label1.Text += " Start ";
    // 1 つのスレッドが終了するまで、呼び出し元のスレッドをブロックする
    thread.Join();
    // シグナル状態になると、メインスレッドのWaitOne()から実行が再開され
    // " End "が表示される
    label1.Text += " End ";
}
SampleThread.cs
using System.Threading;

namespace WindowsFormsApplication1
{
    class SampleThread
    {
        public ManualResetEvent manualResetEvent;

        public SampleThread()
        {
            manualResetEvent = new ManualResetEvent(false);
        }

        public void SampleMethod()
        {
            // manualResetEventを非シグナル状態にする
            manualResetEvent.Reset();
            System.Threading.Thread.Sleep(1500);
            // manualResetEventをシグナル状態にする
            manualResetEvent.Set();
            System.Threading.Thread.Sleep(1000);
        }
    }