Asp.net 定时任务

1.定时器

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Text;
using System.Web.Security;
using System.Web.SessionState;
using System.Timers;namespace WebApplication1
{public class Global : System.Web.HttpApplication{void Application_Start(object sender, EventArgs e){// 在应用程序启动时运行的代码Timer time = new Timer();time.Interval = 1000;time.Enabled = true;time.AutoReset = true;time.Elapsed += new ElapsedEventHandler(TimeEvent);}private void TimeEvent(object source, ElapsedEventArgs e){int intHour = e.SignalTime.Hour;int intMinute = e.SignalTime.Minute;int intSecond = e.SignalTime.Second;int iHour = 10;int iMinute = 30;int iSecond = 00;//每天10:30开始执行程序if (intHour == iHour && intMinute == iMinute && intSecond == iSecond){WriteStream("Timer DateTime:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff") + "\n");} }public void WriteStream(string content){string path = "E:\\hong.txt";FileStream file = new FileStream(path, FileMode.Append);StreamWriter sw = new StreamWriter(file);sw.Write(content);sw.Close();}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,则不会引发该事件。
}}
}

2.cache

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Text;
using System.Web.Security;
using System.Web.SessionState;
using System.Web.Caching;namespace WebApplication1
{public class Global : System.Web.HttpApplication{const string key="key";void Application_Start(object sender, EventArgs e){// 在应用程序启动时运行的代码
HttpRuntime.Cache.Insert(key, "hongda", null, DateTime.Now.AddSeconds(3), Cache.NoSlidingExpiration, CacheItemPriority.Default, new CacheItemRemovedCallback(CachedItemRemoveCallBack));}private void CachedItemRemoveCallBack(string key, object value, CacheItemRemovedReason reason){ WriteStream("Timer DateTime:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff") + "\n");HttpRuntime.Cache.Remove(key);HttpRuntime.Cache.Insert(key, "hongda", null, DateTime.Now.AddSeconds(3), Cache.NoSlidingExpiration, CacheItemPriority.Default, new CacheItemRemovedCallback(CachedItemRemoveCallBack));}public void WriteStream(string content){string path = "E:\\hong.txt";FileStream file = new FileStream(path, FileMode.Append);StreamWriter sw = new StreamWriter(file);sw.Write(content);sw.Close();}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 void CachedItemRemoveCallBack(string key, object value, CacheItemRemovedReason reason){ HttpRuntime.Cache.Remove(key);HttpRuntime.Cache.Insert(key, "hongda", null, DateTime.Now.AddSeconds(3), Cache.NoSlidingExpiration, CacheItemPriority.Default, new CacheItemRemovedCallback(CachedItemRemoveCallBack));WriteStream("Timer DateTime:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff") + "\n"); }

也不对,说明不是writeStream的问题

  void Application_Start(object sender, EventArgs e){// 在应用程序启动时运行的代码HttpRuntime.Cache.Add(key, "hongda", null, DateTime.Now.AddSeconds(3), Cache.NoSlidingExpiration, CacheItemPriority.Default, new CacheItemRemovedCallback(CachedItemRemoveCallBack));}private void CachedItemRemoveCallBack(string key, object value, CacheItemRemovedReason reason){ HttpRuntime.Cache.Remove(key);HttpRuntime.Cache.Add(key, "hongda", null, DateTime.Now.AddSeconds(3), Cache.NoSlidingExpiration, CacheItemPriority.Default, new CacheItemRemovedCallback(CachedItemRemoveCallBack));WriteStream("Timer DateTime:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff") + "\n"); }

还错

   const string key = "key";void Application_Start(object sender, EventArgs e){// 在应用程序启动时运行的代码HttpRuntime.Cache.Add(key, "hongda", null, Cache.NoAbsoluteExpiration ,TimeSpan .FromSeconds (5), CacheItemPriority.Default, new CacheItemRemovedCallback(CachedItemRemoveCallBack));}private void CachedItemRemoveCallBack(string key, object value, CacheItemRemovedReason reason){ HttpRuntime.Cache.Remove(key);HttpRuntime.Cache.Add(key, "hongda", null, Cache.NoAbsoluteExpiration, TimeSpan.FromSeconds(5), CacheItemPriority.Default, new CacheItemRemovedCallback(CachedItemRemoveCallBack));WriteStream("Timer DateTime:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff") + "\n"); }

 

 const string key = "key";void Application_Start(object sender, EventArgs e){// 在应用程序启动时运行的代码HttpRuntime.Cache.Add(key, "hongda", null, Cache.NoAbsoluteExpiration ,TimeSpan.FromMinutes (1), CacheItemPriority.Default, new CacheItemRemovedCallback(CachedItemRemoveCallBack));}private void CachedItemRemoveCallBack(string key, object value, CacheItemRemovedReason reason){if (HttpRuntime .Cache[key] != null){HttpRuntime.Cache.Remove(key);}HttpRuntime.Cache.Add(key, "hongda", null, Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(1), CacheItemPriority.Default, new CacheItemRemovedCallback(CachedItemRemoveCallBack));WriteStream("Timer DateTime:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff") + "\n");}

发现很好

从以前的可以看出最小间隔时间是20s

  const string key = "key";void Application_Start(object sender, EventArgs e){// 在应用程序启动时运行的代码HttpRuntime.Cache.Add(key, "hongda", null, Cache.NoAbsoluteExpiration ,TimeSpan.FromSeconds (20), CacheItemPriority.Default, new CacheItemRemovedCallback(CachedItemRemoveCallBack));}private void CachedItemRemoveCallBack(string key, object value, CacheItemRemovedReason reason){if (HttpRuntime .Cache[key] != null){HttpRuntime.Cache.Remove(key);}HttpRuntime.Cache.Add(key, "hongda", null, Cache.NoAbsoluteExpiration, TimeSpan.FromSeconds(20), CacheItemPriority.Default, new CacheItemRemovedCallback(CachedItemRemoveCallBack));WriteStream("Timer DateTime:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff") + "\n");}public void WriteStream(string content){string path = "E:\\hong.txt";FileStream file = new FileStream(path, FileMode.Append);StreamWriter sw = new StreamWriter(file);sw.Write(content);sw.Close();}

好了

 3.threading

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Text;
using System.Web.Security;
using System.Web.SessionState;
using System.Timers;
using System.Threading;namespace WebApplication1
{public class Global : System.Web.HttpApplication{static AutoResetEvent wait = new AutoResetEvent(false);void Application_Start(object sender, EventArgs e){// 在应用程序启动时运行的代码object state = new object();ThreadPool.RegisterWaitForSingleObject(wait, new WaitOrTimerCallback(test), state, 5000, false);}public void test(object state, bool timedOut){WriteStream("Timer DateTime:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff") + "\n");}public void WriteStream(string content){string path = "E:\\hong.txt";FileStream file = new FileStream(path, FileMode.Append);StreamWriter sw = new StreamWriter(file);sw.Write(content);sw.Close();}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,则不会引发该事件。
}}
}

 static ManualResetEvent wait = new ManualResetEvent(false);        void Application_Start(object sender, EventArgs e){// 在应用程序启动时运行的代码object state = new object();ThreadPool.RegisterWaitForSingleObject(wait, new WaitOrTimerCallback(test), state, 5000, false);}public void test(object state, bool timedOut){WriteStream("Timer DateTime:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff") + "\n");}public void WriteStream(string content){string path = "E:\\hong.txt";FileStream file = new FileStream(path, FileMode.Append);StreamWriter sw = new StreamWriter(file);sw.Write(content);sw.Close();}

 http://www.cnblogs.com/Kazaf/archive/2012/03/26/2417341.html

 

转载于:https://www.cnblogs.com/hongdada/archive/2013/03/26/2983514.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/433764.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

arm64入栈出栈_ARM64的内核栈、用户栈、寄存器上下文

1. 内核栈的分配,即thread_info的分配,是在do_fork->dup_task_struct中分配(默认为2个pages),并赋值给task_struct->stack;2. 用户栈的分配分两种:一是pthread create会事先mmap分配好用户栈,传给do_…

python程序怎么修改_python文件如何修改

以占用内存的方式修改文件 待修改的文件 word1.txt,文件内容如下:现在要将文中的“汪淼”修改为“WM”,代码如下:#!-*-coding:utf-8-*- old_str 汪淼 new_str WM f1 open(word1.txt,r,encoding"utf-8") #…

phpMyAdmin导入大的sql文件

在使用phpMyAdmin的时候经常用到数据的导入和导出(Export/Import),但是在导入大数据的时候由于php上传文件的限制和脚本响应时间的限制,导致phpMyAdmin无法导入大数据,对于导入大数据到mysql的,我以前使用过使用SHELL的方式导入几…

vue 计算属性和data_Vue:计算属性

一、为什么要使用计算属性1、什么是计算属性计算属性:可以理解为能够在里面写一些计算逻辑的属性。具有如下的作用:减少模板中的计算逻辑。数据缓存。当我们的数据没有变化的时候,不会再次执行计算的过程。依赖固定的数据类型(响应式数据)&am…

Linux添加/删除用户和用户组

本文总结了Linux添加或者删除用户和用户组时常用的一些命令和参数。 1、建用户: adduser phpq //新建phpq用户 passwd phpq //给phpq用户设置密码 2、建工作组 groupadd test …

mysql 计算近30天总金额_mysql┃一条更新语句是怎么执行的???

本文共:3018字 预计阅读时间:8分钟文章首发于我的微信公众号:哪儿来的moon,欢迎大家关注mysql┃一条更新语句是怎么执行的???前言 通过上一篇文章的内容,大家已经对mysql的基本架构有…

arduinowifi.send怎么获取响应_ChatterBot代码解读-获取对话

这个过程比较复制,安装处理的流程,依次进行代码解读。在定义一个ChatBot后,可以进行对话的训练,这个过程参考:水中的鱼:ChatterBot代码解读-训练数据​zhuanlan.zhihu.com然后就是用如下的代码&…

[jQuery] jQuery函数

(1)文档就绪函数$(document).ready(function(){--- jQuery functions go here ----});为了防止文档在完全加载(就绪)之前运行 jQuery 代码。如果在文档没有完全加载之前就运行函数,操作可能失败。下面是具体的例子&…

beautifulsoup网页爬虫解析_Python爬虫神器:PyQuery,解析网页更简单,小白也能学会

图/文:迷神我们做python爬虫,通过requests抓取到内容就需要正则匹配,或者其他解析库解析内容。很多可能和我一样的人,都使用jquery的,那用的还是非常爽的。而pyquery库就是jQuery的Python实现,能够以jQuery…

ubuntu设置始终亮屏_ubuntu系统每次启动屏幕都是最大亮度问题的解决方法

这个方法你尝试过没首先你要确保有这个文件:复制代码代码如下:/sys/class/backlight/acpi_video0/brightnesscat出来的值就是你当前的屏幕亮度.我们修改下面的文件加入一些用户启动设置:复制代码代码如下:sudo gedit /etc/rc.local在 exit 0 的上方加入以下代码(确保exit 0没有…

unityios开发--加载视频以及加载完成之后自动跳转 .

在做游戏或者是虚拟漫游一般都会用到在开始的时候加载一段视频,这个视频可能一个介绍整个游戏或者是整个项目的。在加载完了之后自动的跳转到主画面或一个场景,在前在网上百度了一下找到的大部分都是win的好不容易找到了ios的。 Unity3D中播放游戏视频的…

rnn神经网络模型_一文读懂序列建模(deeplearning.ai)之循环神经网络(RNNs)

作者:Pulkit Sharma,2019年1月21日翻译:陈之炎校对:丁楠雅本文为你详细介绍序列模型,并分析其在不同的真实场景中的应用。简介如何预测一个序列中接下来要发生什么事情是一个非常吸引人的课题,这是我对数据…

mysql 人名用什么类型_如何选择合适的MySQL数据类型

一、MySQL数据类型选择原则更小的通常更好:一般情况下选择可以正确存储数据的最小数据类型。越小的数据类型通常更快,占用磁盘,内存和CPU缓存更小。简单就好:简单的数据类型的操作通常需要更少的CPU周期。例如:整型比字…

am335x uart5配置

任务:配置uart5

XML Schema ---complexType-----复合元素

混合的复合类型可包含属性、元素以及文本。 带有混合内容的复合类型 XML 元素&#xff0c;"letter"&#xff0c;含有文本以及其他元素&#xff1a; <letter>Dear Mr.<name>John Smith</name>.Your order <orderid>1032</orderid>will …

Java笔记(一)—StringBuilder类

1、StringBuilder类概述 StringBuilder是一个可变的字符串类&#xff0c;主要指的是StringBuilder对象 中的内容是可变的。与之相比String对象的内容是不变的。2、StringBuilder常用构造方法 public StringBuilder() {} //创建空白可变字符串 public StringBuilder(String st…

ubuntu下制作u盘镜像_deepin下制作win10启动U盘

1.准备①下载multibootusb&#xff1a;http://multibootusb.org/page_download/ ②准备一个win10的镜像文件 ③准备一个U盘&#xff0c;最好先备份U盘数据再格式化一道2.打开multibootusb工具输入开机密码&#xff0c;点击认证3.multibootusb操作部分①选择U盘的第一个分区 ②安…

Shared_from_this 几个值得注意的地方

shared_from_this()是enable_shared_from_this<T>的成员 函数&#xff0c;返回shared_ptr<T>。首先需要注意的是&#xff0c;这个函数仅在shared_ptr<T>的构造函数被调用之后才能使 用。原因是enable_shared_from_this::weak_ptr并不在构造函数中设置&#x…

asterisk1.8 账号信息mysql存储(动态)

1. 首先需要先编译出以下6个模块 res_realtime.so pbx_realtime.so func_realtime.so res_config_mysql.so app_mysql.so cdr_mysql.so 其中有些模块&#xff0c;asterisk默认并不编译&#xff0c;需要修改menuselect.makeopts这个文件&#xff0c;将里面的相应…

签证上的mult是什么意思_申根签证中mult是什么意思

展开全部申根签证中mult是是多次的意思&#xff0c;指可以在有效期内多次往返申根国家。类型申根签证分62616964757a686964616fe78988e69d8331333431373939为入境和过境两类。1.入境签证有一次入境和多次入境两种。签证持有者分别可一次连续停留90天或每半年多次累计不超过3个月…