C#利用System.Threading.Timer实现多线程

using   System;   
using   System.Collections.Generic;   
using   System.Text;   
using   System.Threading;   

 

public class TimerClass : IDisposable

{

    public TimerClass()

    {

        //

        // TODO: Add constructor logic here

        //

    }

 

    private Timer _timer;

    private bool blnDisposed = false;

 

    public void Start()

    {

        _timer = new Timer(new TimerCallback(JobCallBack), null, 1000, 1000);

    }

 

    private void JobCallBack(object state)

    {

        _timer.Change(Timeout.Infinite, Timeout.Infinite);

        Debug.WriteLine("Starting...");

        Thread.Sleep(10000);

        Debug.WriteLine("Complete");

        if (!blnDisposed)

            _timer.Change(1000, 1000);

        else

            _timer = null;

    }

 

    public void Dispose()

    {

        if (_timer != null)

        {

            blnDisposed = true;

            while (_timer != null)

                Thread.Sleep(5);

        }

        Debug.WriteLine("Disposed");

    }

}

posted on 2008-11-26 11:34  RIVERSPIRIT  阅读(8243)  评论(2)    收藏  举报