.net 下的 HttpRuntime.Cache 应用
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Web;
using System.Web.Caching;
using System.Web.Services;
/// <summary>
/// CheckBeat 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消注释以下行。
// [System.Web.Script.Services.ScriptService]
public class CheckBeat : System.Web.Services.WebService {
public CheckBeat () {
//如果使用设计的组件,请取消注释以下行
//InitializeComponent();
}
private static readonly string timeSession = "timebeat";
[WebMethod]
public string Heartbeat()
{
DateTime now = DateTime.Now;
HttpRuntime.Cache.Remove(timeSession);
HttpRuntime.Cache.Add(timeSession, now, null, DateTime.Now.AddMinutes(1), Cache.NoSlidingExpiration, CacheItemPriority.Normal, null);
Thread t = new Thread(new ParameterizedThreadStart(CheckBeats));
t.Start(now);
return "一次心跳完成,记录缓存值:" + now;
}
private void CheckBeats(object time)
{
DateTime now = (DateTime)time;
int times = 1;
for (int i = 1; i <= 10; i++)
{
Thread.Sleep(3000);
object obtime = HttpRuntime.Cache.Get(timeSession);
if (obtime != null)
{
DateTime sessionTime = (DateTime)obtime;
if (sessionTime > now) //心跳正常
{
break;
}
}
times++;
}
if (times > 10) //心跳终止
{
reStart();
//System.Diagnostics.Process.Start(@"D:\TWX\Debug\NewTest.exe");
}
}
private void reStart()
{
string appName = "NewTest";
string appPath = @"D:\TWX\Debug\NewTest.exe";
//send mobile message
try
{
Process[] myPro = Process.GetProcessesByName(appName);
myPro[0].Kill(); //删除进程
}
catch (Exception)
{
}
}
}
最新文章
- Android基础学习第二篇—Activity
- 8.mvc core上传文件
- 股市非常态,CCI指标买卖点实例图解
- 如何搭建maya plugin develop environment on MAC OS X
- 使用hibernate annotation 为非空列加上默认值
- http协议的总结说明
- [转]详解AppDelegate/UIApplication
- SQL注入漏洞和SQL调优SQL注入漏洞和SQL调优
- Jmeter接口测试使用beanshell断言json返回
- Caffe + Ubuntu 15.04 + CUDA 7.0 安装以及配置
- SpringSecurity身份验证基础入门
- 无法解析依赖项。“Microsoft.Net.Http 2.2.29”与 &#39;Microsoft.Net.Http.zh-Hans
- 【GMT43智能液晶模块】例程十二:SDIO实验——读取SD卡信息
- win10专业版密钥 亲测可用 不断更新
- webpack配置接口路径
- gitlab修改默认端口
- 下载YouTube视频的方法
- Eclipse配置C++环境
- Qt监控Arduino开关状态(读取串口数据)
- 去掉每行最后n个字符
热门文章
- access&#160;join形式删除数据
- 如何让thrift0.9.2 在macos上面编译通过?
- JavaWeb-HttpServletResponse对象一
- java中的静态分派和动态分派
- TensorFlow学习笔记(一)--windows系统安装配置
- gcc __attribute__
- python16_day22【cmdb注释】
- Discuz! 论坛
- Divide by Zero 2017 and Codeforces Round #399 (Div. 1 + Div. 2, combined) D. Jon and Orbs
- 【android】 如何把gif图片下载到本地