为什么我写的程序timer中不会自动执行呢?
用的是
this.time1.Tick+=new System.EventHandler(this.time1_Tick);
这边写调用的方法:
private boid time1_Tick(object sender,EventArgs e)
{
} time1 的属性已经设置为True了.
请大家赐教,谢谢...
用的是
this.time1.Tick+=new System.EventHandler(this.time1_Tick);
这边写调用的方法:
private boid time1_Tick(object sender,EventArgs e)
{
} time1 的属性已经设置为True了.
请大家赐教,谢谢...
解决方案 »
- 如果我使用SerialPort发送获取数据,未使用SerialPort的DataReceived事件会有什么问题???
- 。。。只要懂一点的都知道
- 有关HtmlElement.OuterHtml的问题,高手进
- 请问vs2005中有没有类似vb中的Drivelistbox和Dirlistbox这样的控件?
- 20分求解:用C#如何将WAV格式音频转换为mp3格式?
- winform 下的DataGrid如何实现合并单元格?
- 持续构建工具Jenkins(hodson)无法读出报告问题
- 如何获得异常的级别 比如System.NullReferenceException这个异常究竟是ERROR还是Warn
- 急急!!!请问在C#中如何将查询结果赋值??谢谢!!!
- VS.net安装问题
- C#如何能快速读取excel
- c#正则验证中特别纠结的一个问题
using System;namespace CodeGuru.TimerService
{
/// <summary>
/// Demonstrate the use of a timer.
/// </summary>
public class TimedItem
{
private System.Diagnostics.EventLog _AppEventLog;
private System.Timers.Timer _Timer; private int _Interval = 30000;
public int Interval
{
get { return this._Interval; }
set { this._Interval = value; }
} private string _Name = "";
public string Name
{
get { return this._Name; }
set { this._Name = value; }
} /// <summary>
/// Constructor
/// </summary>
public TimedItem(System.Diagnostics.EventLog AppEventLog)
{
this._Timer = new System.Timers.Timer();
this._Timer.Elapsed += new
System.Timers.ElapsedEventHandler(_Timer_Elapsed);
this._Timer.Enabled = false;
this._Timer.Interval = this.Interval;
this._AppEventLog = AppEventLog;
} /// <summary>
/// Start the timer.
/// </summary>
public void StartTimer()
{
this._Timer.Enabled = true;
} /// <summary>
/// Stop the timer.
/// </summary>
public void StopTimer()
{
this._Timer.Enabled = false;
}
/*
* Respond to the _Timer elapsed event.
*/
private void _Timer_Elapsed(object sender,
System.Timers.ElapsedEventArgs e)
{
this._AppEventLog.WriteEntry("Time elapsed for " + this.Name,
System.Diagnostics.EventLogEntryType.Information);
}
}
}//windows service 类using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Threading;namespace CodeGuru.TimerService
{
public class TimerService : System.ServiceProcess.ServiceBase
{
private System.Diagnostics.EventLog _AppEventLog;
private Thread[] _WorkerThreads;
private TimedItem[] _Timers; /// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null; public TimerService()
{
// This call is required by the Windows.Forms Component
// Designer.
InitializeComponent(); // TODO: Add any initialization after the InitComponent
// call
} // The main entry point for the process
static void Main()
{
System.ServiceProcess.ServiceBase[] ServicesToRun;
ServicesToRun = new System.ServiceProcess.ServiceBase[] {
new TimerService() }; System.ServiceProcess.ServiceBase.Run(ServicesToRun);
} /// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this._AppEventLog = new System.Diagnostics.EventLog();
((System.ComponentModel.ISupportInitialize)
(this._AppEventLog)).BeginInit();
//
// _AppEventLog
//
this._AppEventLog.Log = "Application";
this._AppEventLog.Source = "TimerService";
//
// TimerService
//
this.ServiceName = "TimerService";
((System.ComponentModel.ISupportInitialize)
(this._AppEventLog)).EndInit();
} /// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} /// <summary>
/// Set things in motion so your service can do its work.
/// </summary>
protected override void OnStart(string[] args)
{
this._Timers = new TimedItem[5];
_WorkerThreads = new Thread[5]; for( int i = 0; i < 5; i++ )
{
_Timers[i] = new TimedItem(this._AppEventLog);
_Timers[i].Name = "Timer #" + i.ToString();
_WorkerThreads[i] = new Thread(new
System.Threading.ThreadStart(_Timers[i].StartTimer));
_WorkerThreads[i].Start();
}
} /// <summary>
/// Stop this service.
/// </summary>
protected override void OnStop()
{
for( int i = 0; i < 5; i++ )
{
_Timers[i].StopTimer();
}
}
}
}
参考:
http://www.codeguru.com/columns/dotnet/article.php/c6919__1/Using-Timers-in-a-Windows-Service.htm
而不是System.Window.Form.Timer