view Test/RewritingTest/CreateThread.cs @ 21:d488eb23a29f

add ThreadTest
author riono <e165729@ie.u-ryukyu.ac.jp>
date Thu, 03 Dec 2020 19:04:33 +0900
parents
children
line wrap: on
line source

using System;
using System.Threading;

public class CreateThread {
    public static void Main () {
        Console.WriteLine ("スタート");

        // DoSomethingメソッドを別のスレッドで実行するThreadオブジェクトを作成する
        //Thread t = new Thread (new ThreadStart (DoSomething));
        // スレッドを開始する
        //t.Start ();

        //t.IsBackground = true;

        //t.Join ();

        CreateThread createThread = new CreateThread();
        createThread.Run();
        
        Console.WriteLine ("Enterキーを押してください");
        Console.ReadLine ();
    }

    public void Run() {
        Thread thread = new Thread(DoSomething);
        thread.Start();
        thread.Join();
    }

    // 別スレッドで実行するメソッド
    private static void DoSomething () {
        // 長い時間のかかる処理があるものとする
        for (long i = 0; i < 1000000; i++) {
            Console.Write("1");
        }

        // 処理が終わったことを知らせる
        Console.WriteLine ("終わりました");
    }
}