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)
    {

    }
    }

    }

  • 相关阅读:
    UML类图(上):类、继承和实现
    Maven实战:Maven生命周期
    MyBatis6:MyBatis集成Spring事物管理(下篇)
    MyBatis5:MyBatis集成Spring事务管理(上篇)
    Spring7:基于注解的Spring MVC(下篇)
    Spring6:基于注解的Spring MVC(上篇)
    Spring5:@Autowired注解、@Resource注解和@Service注解
    Dubbo学习小记
    Maven入门详解
    MyBatis4:动态SQL
  • 原文地址:https://www.cnblogs.com/chengjun/p/9253676.html
Copyright © 2011-2022 走看看