0

スレッドのタイマーについて質問がありますか?

私のコード:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
       Testing testing = new Testing();           
       Thread thread = new Thread(new ThreadStart(delegate
        {
            testing.Start();               
        }));
        thread.IsBackground = true;
        thread.Start();
    }


}

public class Testing
{
    System.Threading.Timer timer = null;

    public void Start()
    {
        if (timer == null)
            timer = new System.Threading.Timer(timerTick, null, 500, 500);
        List<ManualResetEvent> amanual = new List<ManualResetEvent>();
        for (int index = 0; index < 10; index++)
        {
            ManualResetEvent manual = new ManualResetEvent(false);
            ThreadPool.QueueUserWorkItem(new WaitCallback(delegate(object context)
                {
                    this.RunningThreadPool(context);
                    manual.Set();
                }
                ));
            amanual.Add(manual);
        }

        WaitHandle.WaitAll(amanual.ToArray());
        Console.WriteLine("Task complete");
    }
    void timerTick(object sender)
    {            
        Console.WriteLine("Timer running");
    }

    public void RunningThreadPool(object context)
    {            
        Console.WriteLine("Thread in Threadpool is running...");           
    }
}

}

そして、これが結果です Thread in Threadpool is running... Thread in Threadpool is running... Thread in Threadpool is running... Thread in Threadpool is running... Thread in Threadpool is running... Thread in Threadpool is running. .. スレッドプール内のスレッドが実行中です... スレッドプール内のスレッドが実行中です... スレッドプール内のスレッドが実行中です... スレッドプール内のスレッドが実行中です... タスク完了 タイマー実行中 タイマー実行中 タイマー実行中 タイマー実行中

スレッドプールが実行される前にタイマーがどのように実行されるのかという質問があります。スレッドプールにタイマーを設定することを除いて。スレッドプールのタイマーチェックスレッドが必要だからです。

4

1 に答える 1