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