Files
UnrealEngineUWP/Engine/Source/Programs/AutomationTool/AutomationUtils/WatchdogTimer.cs
T

88 lines
2.0 KiB
C#
Raw Normal View History

2014-12-07 19:09:38 -05:00
// Copyright 1998-2015 Epic Games, Inc. All Rights Reserved.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Timers;
using System.Runtime.CompilerServices;
using UnrealBuildTool;
namespace AutomationTool
{
/// <summary>
/// Timer that exits the application of the execution of the command takes too long.
/// </summary>
2014-05-13 14:31:45 -04:00
public class WatchdogTimer : IDisposable, IProcess
{
private Timer CountownTimer;
private System.Diagnostics.StackFrame TimerFrame;
2014-05-13 14:31:45 -04:00
private object SyncObject = new object();
private bool bTicking = false;
[MethodImplAttribute(MethodImplOptions.NoInlining)]
public WatchdogTimer(int Seconds)
{
TimerFrame = new System.Diagnostics.StackFrame(1);
CountownTimer = new Timer(Seconds * 1000.0);
CountownTimer.Elapsed += Elapsed;
CountownTimer.Start();
2014-05-13 14:31:45 -04:00
bTicking = true;
ProcessManager.AddProcess(this);
}
void Elapsed(object sender, ElapsedEventArgs e)
{
2014-05-13 14:31:45 -04:00
lock (SyncObject)
{
bTicking = false;
}
int Interval = (int)(CountownTimer.Interval * 0.001);
Dispose();
var Method = TimerFrame.GetMethod();
2014-05-13 14:31:45 -04:00
Log.TraceError("BUILD FAILED: WatchdogTimer timed out after {0}s in {1}.{2}", Interval, Method.DeclaringType.Name, Method.Name);
Environment.Exit(1);
}
public void Dispose()
{
2014-05-13 14:31:45 -04:00
Log.TraceInformation("WatchdogTimer.Dispose()");
lock (SyncObject)
{
2014-05-13 14:31:45 -04:00
if (CountownTimer != null)
{
CountownTimer.Stop();
CountownTimer.Dispose();
CountownTimer = null;
}
bTicking = false;
}
2014-05-13 14:31:45 -04:00
ProcessManager.RemoveProcess(this);
}
public void StopProcess(bool KillDescendants = true)
{
Dispose();
}
public bool HasExited
{
get
{
bool bResult = true;
lock (SyncObject)
{
bResult = bTicking;
}
return bResult;
}
}
public string GetProcessName()
{
var Method = TimerFrame.GetMethod();
return String.Format("WatchdogTimer_{0}.{1}", Method.DeclaringType.Name, Method.Name);
}
}
}