各位XDJM帮忙看有办法解决不!

解决方案 »

  1.   

    贴出来看看,不然怎么知道你的timer是怎么工作的?
    还有如果控制timer的线程结束了,相应的那个timer也就不再工作
      

  2.   

    using System;
    using System.Data ;
    using System.Web ;
    using System.Threading ;
    using IMC.Data ;
    using IMC.Business .LogManage ;namespace Sub_Task_Send_Service
    {
    /// <summary>
    /// Sub_Task_Send_Main 的摘要说明。
    /// </summary>
    public class Sub_Task_Send_Main
    {
    protected System.Timers.Timer timer1;
    protected System.Timers.Timer timer2;
    //始终的时间间隔变量,单位是毫秒
    public static int Sub_Task_Scantime=60000;//1800000;
    public static int ThreadAttemperTime=180000;//1800000;
    //调度程序是否运行
    public static bool isrun=false;
    public  Sub_Task_Scan sub_task_scan=new Sub_Task_Scan() ;
    public  Sub_Task_Run_Thread sub_task_run_thread=new Sub_Task_Run_Thread(); public static Sub_Task_Send_Main subtask_sendmain=null;
    public Sub_Task_Send_Main()
    {
    //
    // TODO: 在此处添加构造函数逻辑
    //
    MainControl();
    }
    public void MainControl()
    {
    LogManager logManager=new LogManager ();
    if(!isrun)
    System.Console.Write ("正在初始化系统...");
    try
    {
    if(isrun)
    {
    System.Console .Write ("调度启动成功!");
    }
    else
    {
    this.timer1 =new System.Timers.Timer ();
    this.timer1 .Enabled =true;
    this.timer1.Elapsed += new System.Timers.ElapsedEventHandler(this.timer1_Elapsed);
    this.timer1 .Interval =Sub_Task_Scantime;
    this.timer2 =new System.Timers.Timer ();
    this.timer2 .Enabled =true;
    this.timer2.Elapsed += new System.Timers.ElapsedEventHandler(this.timer2_Elapsed);
    this.timer2 .Interval =ThreadAttemperTime;
    sub_task_run_thread.RunTask();
    } }
    catch(Exception err)
    {
    isrun=false;
    logManager.Insert_SysLog (err);
    }
    }
    private void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
    {
    sub_task_scan.Select_Sub_Task();
    }
    private void timer2_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
    {
    sub_task_run_thread.RunTask ();
            }
    }
    }
      

  3.   

    代码上看不出什么问题!说点建议,不知道对错
    你的控制用一个time就够了,time很消耗系统资源,做个标量记录time被触发的次数,当被触发3次,执行time2的操作就可以了,然后标量清0,从新开始记录
      

  4.   

    同意,一个线程里面最好不要用多个不同周期的timer。
    你可以在你的timer事件处理函数中对timer计数。应该很简单的,例如,你可以在类中先定义一个全局变量:
    public static int iTimerCount=0;
    然后在函数里面计数呀:
    private void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
    {
             iTimerCount++;                  
    sub_task_scan.Select_Sub_Task();
             if iTimerCount>2
             {
                  sub_task_run_thread.RunTask ();
                  iTimerCount=0;
              }
    }
      

  5.   


    比如第二个timer的周期是第一个的三倍,你就用
    {
    int static Count=0;       
               
    sub_1();
             if (Count/2!=0)
             {
                  sub2();
                  Count=0;
              }
    Count++;
    }
    多线程里面尽量少用timer,单线程,尽力只用一个timer!
      

  6.   

    说明一下可能会出现的问题:我的程序中 sub_task_run_thread.RunTask ()  是一个包含多线程的方法,这样使用的话就不就变成了单线程了,而且在RunTask () 中可能执行时间会很长,这时  sub_task_scan.Select_Sub_Task()   不也停止了,要等RunTask () 执行完才能执行  Select_Sub_Task() 呢?
      

  7.   

    如果两个timer的周期相同的话,是不适就不会产生如此的问题呢?
      

  8.   

    其它线程和当前线程没有等待关系,所以这样写应该不是一个单线程的程序。其实,你可以自己调试下看看就知道了。你可以在当前线程、其他thread中的代码中同时加入一些存储文字到文件或者事件查看器的代码,运行一下看看就知道了。你会发现:多个线程是“交叉”执行的。