public bool IsRunning { get{return _isRunning;} } public DateTime LastStarted { get{ return _lastStart;} } public DateTime LastEnd { get{ return _lastEnd;} } public DateTime LastSuccess { get{ return _lastSucess;} }
public bool SingleThreaded { get{ return _singleThread;} } /// <summary> /// Named type of class which implements IJob /// </summary> public Type JobType { get { return this._jobType; }
} public int Minutes { get{return _minutes;} set{_minutes = value;} } /// <summary> /// On an exception, should this job be deactivated /// </summary> public bool EnableShutDown { get { return this._enableShutDown; } } /// <summary> /// Name of Job /// </summary> public string Name { get { return this._name; } }
/// <summary> /// Is this job enabled /// </summary> public bool Enabled { get { return this._enabled; } } /// <summary> /// Attempts to create an instance of the IJob. If the type /// can not be created, this Job will be disabled. /// </summary> /// <returns></returns> public IJob CreateJobInstance() { if(Enabled) { if(_ijob == null) {
只有第一次访问web应用的时候,web应用对象才被实例化。因此你说的那种东西与asp.net无缘。你可以让一个远程程序不断访问你的asp.net网站,不过这有点......
// <copyright company="Telligent Systems">
// Copyright (c) Telligent Systems Corporation. All rights reserved.
// </copyright>
//------------------------------------------------------------------------------using System;
using System.Threading;
using System.Xml;
using System.Xml.Serialization;
using CommunityServer.Components;namespace CommunityServer.Configuration
{
/// <summary>
/// Configurable IJob descritpion
/// </summary>
[Serializable]
[XmlRoot("job")]
public class Job : IDisposable
{
public event EventHandler PreJob;
public event EventHandler PostJob; private void OnPreJob()
{
try
{
if(PreJob != null)
PreJob(this,EventArgs.Empty);
}
catch(Exception ex)
{
EventLogs.Warn("PreJob listener failed", "Jobs", 807,ex, CSContext.Current.SettingsID);
}
} private void OnPostJob()
{
try
{
if(PostJob != null)
PostJob(this,EventArgs.Empty);
}
catch(Exception ex)
{
EventLogs.Warn("Post listener failed", "Jobs", 807,ex, CSContext.Current.SettingsID);
}
} public Job(Type ijob, XmlNode node)
{
_node = node; _jobType = ijob; XmlAttribute att = node.Attributes["enabled"];
if(att != null)
this._enabled = bool.Parse(att.Value); att = node.Attributes["enableShutDown"];
if(att != null)
this._enableShutDown = bool.Parse(att.Value); att = node.Attributes["name"];
if(att != null)
this._name = att.Value; att = node.Attributes["seconds"];
if(att != null)
{
_seconds = Int32.Parse(att.Value);
} att = node.Attributes["firstRun"];
if(att != null)
{
_firstRun = Int32.Parse(att.Value);
} att = node.Attributes["minutes"];
if(att != null)
{
try
{
this._minutes = Int32.Parse(att.Value);
}
catch
{
this._minutes = 15;
}
} att = node.Attributes["singleThread"];
if(att != null && !Globals.IsNullorEmpty(att.Value) && string.Compare(att.Value,"false", false) == 0)
_singleThread = false;
} #region Private Members
private IJob _ijob;
private bool _enabled = true;
private Type _jobType;
private string _name;
private bool _enableShutDown = false;
private int _minutes = 15;
private Timer _timer = null;
private bool disposed = false;
private XmlNode _node = null;
private bool _singleThread = true;
private DateTime _lastStart;
private DateTime _lastSucess;
private DateTime _lastEnd;
private bool _isRunning;
private int _seconds = -1;
private int _firstRun = -1; protected int Interval
{
get
{
if(_firstRun > 0)
return _firstRun * 1000; if(_seconds > 0)
return _seconds * 1000; return Minutes * 60000;
}
} #endregion
/// <summary>
/// Creates the timer and sets the callback if it is enabled
/// </summary>
public void InitializeTimer()
{
if(_timer == null && Enabled)
{
_timer = new Timer(new TimerCallback(timer_Callback), null,Interval, Interval);
}
} /// <summary>
/// Internal call back which is responsible for firing IJob.Execute()
/// </summary>
/// <param name="state"></param>
private void timer_Callback(object state)
{
// Guid id = (Guid)state;
// if(id != Jobs.Instance().CurrentID)
// {
// this.Dispose();
// return;
// } if(!Enabled)
return; _timer.Change( Timeout.Infinite, Timeout.Infinite ); _firstRun = -1; ExecuteJob(); if(Enabled)
_timer.Change( Interval, Interval);
else
this.Dispose(); } public void ExecuteJob()
{
OnPreJob(); _isRunning = true;
IJob ijob = this.CreateJobInstance();
if(ijob != null)
{
_lastStart = DateTime.Now;
try
{
ijob.Execute(this._node);
_lastEnd = _lastSucess = DateTime.Now;
}
catch(Exception)
{
this._enabled = !this.EnableShutDown;
_lastEnd = DateTime.Now;
}
}
_isRunning = false; OnPostJob();
}
public bool IsRunning
{
get{return _isRunning;}
} public DateTime LastStarted
{
get{ return _lastStart;}
} public DateTime LastEnd
{
get{ return _lastEnd;}
} public DateTime LastSuccess
{
get{ return _lastSucess;}
}
public bool SingleThreaded
{
get{ return _singleThread;}
} /// <summary>
/// Named type of class which implements IJob
/// </summary>
public Type JobType
{
get { return this._jobType; }
} public int Minutes
{
get{return _minutes;}
set{_minutes = value;}
}
/// <summary>
/// On an exception, should this job be deactivated
/// </summary>
public bool EnableShutDown
{
get { return this._enableShutDown; }
}
/// <summary>
/// Name of Job
/// </summary>
public string Name
{
get { return this._name; }
}
/// <summary>
/// Is this job enabled
/// </summary>
public bool Enabled
{
get { return this._enabled; }
}
/// <summary>
/// Attempts to create an instance of the IJob. If the type
/// can not be created, this Job will be disabled.
/// </summary>
/// <returns></returns>
public IJob CreateJobInstance()
{
if(Enabled)
{
if(_ijob == null)
{
if(_jobType != null)
{
_ijob = Activator.CreateInstance(_jobType) as IJob;
}
_enabled = (_ijob != null); if(!_enabled)
this.Dispose();
}
}
return _ijob;
}
#endregion #region IDisposable Members public void Dispose()
{
if(_timer!= null && !disposed)
{
lock(this)
{
_timer.Dispose();
_timer = null;
disposed = true;
}
}
} #endregion
}
}
然后再找一个装可以服务的机器,写个windows服务,定时去调用web service
{
}然后在类型的static实例化方法中写: System.Threading.Timer tm=new System.Threading.Timer(
new System.Threading.TimerCallback(doit),null,90000,90000);这就可以每隔90秒钟执行一次doit方法。你可以在global.ascx或者任何类中写这些。测试的时候可以把90000改为小一些的值,并且在你的主程序中写
System.Threading.Thread.Sleep(0);
这样就可以看到主程序执行的时候doit被并行执行了。
讲述一个static的定时程序不必那么神秘,实用是最主要的。一个timer可以这样使用:
-----------------
我因为对这一块也不怎么熟悉,所以只好把CS上的代码直接贴上来,让你见笑了,哈哈~
{
static void doit(object state)
{
System.IO.File.AppendAllText("c:\\test_timer.txt","\r\n"+ DateTime.Now.ToString());
} static myTestTimer()
{
System.Threading.Timer tm = new System.Threading.Timer(
new System.Threading.TimerCallback(doit), null, 2000, 2000);
}
}然后随便找一个网页在page_load里写上一句: myTestTimer t=new myTestTimer();就可以了。
static void doit(object state)
{
System.IO.File.AppendAllText("c:\\test_timer.txt", "\r\n" + DateTime.Now.ToString());
}static System.Threading.Timer tm;void Application_Start(object sender, EventArgs e)
{
tm = new System.Threading.Timer(new System.Threading.TimerCallback(doit), null, 2000, 2000);
}
sql server的作业里可不可以实现跳转的功能?
luckbird(luckbird)
我想不通过客户端的触发而实现自动定时执行程序,而不需要在服务器写服务.
——————————————————————————————————————
通常如果你的网站被禁止启动你的windows服务,那么大概都会禁止sql server的agent服务。如果允许,并且系统dba管理员也帮你配置和管理,dba通常也会将sql server中一堆非常不安全的存储过程(例如xp_cmdshell)删除掉,不可能允许你随意乱搞服务器。
protected void Application_Start(Object sender, EventArgs e)
{
Thread.Start();
}