.NET2.0 WEB时定时执行任务解决方案 稳定性求解!
运行了一周时间,其中有两次没有执行,大家看看该如何使其能够稳定的运行呢? 我现在没有好的办法啊
<%@ Application Language="C#" %>
<%@ import Namespace="System.IO" %>
<%@ import Namespace="System.Data.SqlClient" %>
<script runat="server">
void Application_Start(object sender, EventArgs e)
{
//在应用程序启动时运行的代码
System.Timers.Timer myTimer = new System.Timers.Timer();
myTimer.Elapsed += new System.Timers.ElapsedEventHandler(OnTimedEvent);
myTimer.Interval = 1000;
myTimer.Enabled = true;
}
void Application_End(object sender, EventArgs e)
{
//在应用程序关闭时运行的代码
}
void Application_Error(object sender, EventArgs e)
{
//在出现未处理的错误时运行的代码
}
void Session_Start(object sender, EventArgs e)
{
//在新会话启动时运行的代码
}
void Session_End(object sender, EventArgs e)
{
//在会话结束时运行的代码。
// 注意: 只有在 Web.config 文件中的 sessionstate 模式设置为
// InProc 时,才会引发 Session_End 事件。如果会话模式
//设置为 StateServer 或 SQLServer,则不会引发该事件。
}
private static void OnTimedEvent(object source, System.Timers.ElapsedEventArgs e)
{
//string strweek = e.SignalTime.DayOfWeek.ToString();
int inthour = e.SignalTime.Hour;
int intminute = e.SignalTime.Minute;
int intSecond = e.SignalTime.Second;
//string sweek = "Thursday";
int ihour = 21;
int iminute = 00;
int isecond = 00;
if (inthour==ihour && intminute==iminute && intSecond == isecond)
{
string connectionString = ConfigurationManager.ConnectionStrings["TestforInsertConnectionString"].ConnectionString;
SqlConnection myConnection = new SqlConnection(connectionString);
string sql = "insert into line(ID,LName)values('20','line01')";
SqlCommand myCommand = new SqlCommand(sql, myConnection);
myConnection.Open();
myCommand.ExecuteNonQuery();
myConnection.Close();
}
}
</script>