关于多线程中的timer事件问题!!!(十万火急,送高分) 各位XDJM帮忙看有办法解决不! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 贴出来看看,不然怎么知道你的timer是怎么工作的?还有如果控制timer的线程结束了,相应的那个timer也就不再工作 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 (); } }} 代码上看不出什么问题!说点建议,不知道对错你的控制用一个time就够了,time很消耗系统资源,做个标量记录time被触发的次数,当被触发3次,执行time2的操作就可以了,然后标量清0,从新开始记录 同意,一个线程里面最好不要用多个不同周期的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; }} 比如第二个timer的周期是第一个的三倍,你就用{int static Count=0; sub_1(); if (Count/2!=0) { sub2(); Count=0; }Count++;}多线程里面尽量少用timer,单线程,尽力只用一个timer! 说明一下可能会出现的问题:我的程序中 sub_task_run_thread.RunTask () 是一个包含多线程的方法,这样使用的话就不就变成了单线程了,而且在RunTask () 中可能执行时间会很长,这时 sub_task_scan.Select_Sub_Task() 不也停止了,要等RunTask () 执行完才能执行 Select_Sub_Task() 呢? 如果两个timer的周期相同的话,是不适就不会产生如此的问题呢? 其它线程和当前线程没有等待关系,所以这样写应该不是一个单线程的程序。其实,你可以自己调试下看看就知道了。你可以在当前线程、其他thread中的代码中同时加入一些存储文字到文件或者事件查看器的代码,运行一下看看就知道了。你会发现:多个线程是“交叉”执行的。 高难度,请教lamda表达式 用那些语言可以和手机通讯 vs中如何记录鼠标单击的次数 有谁能帮我js代码改写成C# C#做窗体应用程序时 picturebox 怎样同时显示多张图片 要用winform做一套业务管理系统,求winform得源代码。 请教 帅哥美女看过来!Button_Click事件中启动线程有问题! 像"Nhiberate"这些框架的实体绑定到dataGrid,它们在后台怎么样绑定到datagrid的记录那些是新增的、修改的、删除的 请问如何遍历窗体的控件,并判断是什么控件! 熟悉网络嗅探器的请进!!!! 在一个类中,怎样确定该定义成员变量为静态成员变量或者是实例成员变量??
还有如果控制timer的线程结束了,相应的那个timer也就不再工作
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 ();
}
}
}
你的控制用一个time就够了,time很消耗系统资源,做个标量记录time被触发的次数,当被触发3次,执行time2的操作就可以了,然后标量清0,从新开始记录
你可以在你的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;
}
}
比如第二个timer的周期是第一个的三倍,你就用
{
int static Count=0;
sub_1();
if (Count/2!=0)
{
sub2();
Count=0;
}
Count++;
}
多线程里面尽量少用timer,单线程,尽力只用一个timer!