需求是:winform定时读取文件到数据库问题:由于文件比较大执行读取很慢,所以我创建了一个新的线程去执行读取。而又需要定时读取,我就用了一个定时器,定时去创建这个线程。而这样的话,如果执行时间超过设定的定时时间就会在我结束之前创建的线程之前又创建了一个新的读取线程,这样就发生了争抢资源的问题要如何才能不在要创建的线程已经存在的情况下不另外创建新的呢(不是如何解决争抢资源的问题!)

解决方案 »

  1.   

    创建新的线程之前检查一下原来的线程是否IsAlive,true的话说明还没有结束。
      

  2.   

    private void timer1_Tick(object sender, System.EventArgs e)
    {
    this.thread = new Thread( new ThreadStart(this.readRules));
    this.thread.Name = "DownloadInfo";
    if(!this.thread.IsAlive)
    {
    this.thread.Start();
    }我是这么做的
    该怎么样才能得到上次产生的线程的句柄呢
      

  3.   

    应该这样吧
    private void timer1_Tick(object sender, System.EventArgs e)
    {
             if((this.thread == null)||(!this.thread.IsAlive))
    {
             this.thread = new Thread( new ThreadStart(this.readRules));
             this.thread.Name = "DownloadInfo";
    this.thread.Start();
    }
    }
      

  4.   

    你加个 synclock 不就好了?