zoukankan      html  css  js  c++  java
  • .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:TWXDebugNewTest.exe");
    }
    }

    private void reStart()
    {
    string appName = "NewTest";
    string appPath = @"D:TWXDebugNewTest.exe";

    //send mobile message
    try
    {
    Process[] myPro = Process.GetProcessesByName(appName);
    myPro[0].Kill(); //删除进程
    }
    catch (Exception)
    {

    }
    }

    }

  • 相关阅读:
    第十一节 jQuery特殊效果
    第十节 使用index和一个点击事件实现选项卡
    synchronized和lock两种锁的比较
    常见的四种线程池和区别
    mybatis中的#和$的区别
    web 防止SQL注入
    GIT配置免密登录
    热点 Key 问题的发现与解决
    Redis缓存击穿
    面试必问之JVM原理
  • 原文地址:https://www.cnblogs.com/chengjun/p/9253676.html
Copyright © 2011-2022 走看看