C# 每n分钟执行一个异步方法

C# 每n分钟执行一个异步方法,c#,asp.net,C#,Asp.net,我已经实现了一个http模块,它将在ASP.NET应用程序启动时启动 using System.Web; using System.Threading.Tasks; using System; using System.Net.Http; namespace BL.HttpModules { public class MyCustomAsyncModule : IHttpModule { #region Static Privates priv

我已经实现了一个http模块,它将在ASP.NET应用程序启动时启动

using System.Web;
using System.Threading.Tasks;
using System;
using System.Net.Http;

namespace BL.HttpModules
{
    public class MyCustomAsyncModule : IHttpModule
    {
        #region Static Privates

        private static bool applicationStarted = false;
        private readonly static object applicationStartLock = new object();

        #endregion

        public void Dispose()
        {

        }

        /// <summary>
        /// Initializes the specified module.
        /// </summary>
        /// <param name="httpApplication">The application context that instantiated and will be running this module.</param>
        public void Init(HttpApplication httpApplication)
        {
            if (!applicationStarted)
            {
                lock (applicationStartLock)
                {
                    if (!applicationStarted)
                    {
                        // this will run only once per application start
                         this.OnStart(httpApplication);
                    }
                }
            }
            // this will run on every HttpApplication initialization in the application pool
            this.OnInit(httpApplication);
        }

        public virtual void OnStart(HttpApplication httpApplication)
        {            
            httpApplication.AddOnBeginRequestAsync(OnBegin, OnEnd);
        }

        private IAsyncResult OnBegin(object sender, EventArgs e, AsyncCallback cb, object extraData)
        {
            applicationStarted = true;
            var tcs = new TaskCompletionSource<object>(extraData);
            DoAsyncWork(HttpContext.Current).ContinueWith(t =>
            {
                if (t.IsFaulted)
                {
                    tcs.SetException(t.Exception.InnerExceptions);
                }
                else
                {
                    tcs.SetResult(null);
                }
                if (cb != null) cb(tcs.Task);
            });
            return tcs.Task;
        }

        async Task DoAsyncWork(HttpContext ctx)
        {
            var client = new HttpClient();
            var result = await client.GetStringAsync("http://google.com");
            // USE RESULT
        }

        private void OnEnd(IAsyncResult ar)
        {
            Task t = (Task)ar;
            t.Wait();
        }

        /// <summary>Initializes any data/resources on HTTP module start.</summary>
        /// <param name="httpApplication">The application context that instantiated and will be running this module.</param>
        public virtual void OnInit(HttpApplication httpApplication)
        {
            // put your module initialization code here


        }

    }// end class
}// end namespace
使用System.Web;
使用System.Threading.Tasks;
使用制度;
使用System.Net.Http;
命名空间BL.HttpModules
{
公共类MyCustomAsyncModule:IHttpModule
{
#区域静态私处
私有静态bool应用程序启动=false;
私有只读静态对象应用程序startlock=new object();
#端区
公共空间处置()
{
}
/// 
///初始化指定的模块。
/// 
///实例化并将运行此模块的应用程序上下文。
public void Init(HttpApplication HttpApplication)
{
如果(!applicationStarted)
{
锁(应用程序启动锁)
{
如果(!applicationStarted)
{
//每次应用程序启动仅运行一次
此.OnStart(httpApplication);
}
}
}
//这将在应用程序池中的每个HttpApplication初始化上运行
本文件为OnInit(httpApplication);
}
公共虚拟void OnStart(HttpApplication HttpApplication)
{            
httpApplication.AddOnBeginRequestAsync(OnBegin,OnEnd);
}
私有IAsyncResult OnBegin(对象发送方、事件参数、异步回调cb、对象外部数据)
{
applicationStarted=true;
var tcs=新任务完成源(extraData);
DoAsyncWork(HttpContext.Current).ContinueWith(t=>
{
如果(t.IsFaulted)
{
SetException(t.Exception.innerException);
}
其他的
{
tcs.SetResult(空);
}
如果(cb!=null)cb(tcs.Task);
});
返回tcs.Task;
}
异步任务DoAsyncWork(HttpContext ctx)
{
var client=新的HttpClient();
var result=await client.GetStringAsync(“http://google.com");
//使用结果
}
私有无效OnEnd(IAsyncResult ar)
{
任务t=(任务)ar;
t、 等待();
}
///初始化HTTP模块启动时的所有数据/资源。
///实例化并将运行此模块的应用程序上下文。
公共虚拟无效OnInit(HttpApplication HttpApplication)
{
//将模块初始化代码放在这里
}
}//末级
}//结束命名空间

我想在每5分钟后解雇一名员工。您能在该模块中帮助我实现这一目标吗?

IIS中没有可靠的内置方法,您需要使用外部进程或第三方库来安排要完成的工作。是一个非常流行的库,允许您执行进程内和进程外的计划任务。

您知道asp.net中的任何类型的计划任务都是一个坏主意,不是吗?@Amy的可能重复我不同意特定的重复,这是asp.net的事实改变了您执行重复工作的限制,由于应用程序池回收,链接副本中的两个解决方案在ASP.NET上无法长期工作。