public Task RequestClose(LazyResourceManager requester) { lock (LockObj) { ReferenceCount--; if (ReferenceCount == 0) { switch (state) { case ResourceState.Reset: case ResourceState.Closing: throw new Exception("Should never happen"); case ResourceState.Opening: case ResourceState.Open: { state = ResourceState.Closing; return(CloseTask = Task.Factory.StartNew(() => { try { // wait for the resource to open before close. requester.resources[ResourceNode.Resource].OpenTask?.Wait(); } catch { } Task.WaitAll(ResourceNode.WeakDependencies.Select(requester.RequestResourceClose).ToArray()); var reslog = ResourceTaskManager.GetLogSource(ResourceNode.Resource); Stopwatch timer = Stopwatch.StartNew(); try { ResourceNode.Resource.Close(); } catch (Exception e) { reslog.Error("Error while closing \"{0}\": {1}", ResourceNode.Resource.Name, e.Message); reslog.Debug(e); } reslog.Info(timer, "Resource \"{0}\" closed.", ResourceNode.Resource); Task.WaitAll(ResourceNode.StrongDependencies.Select(requester.RequestResourceClose).ToArray()); state = ResourceState.Reset; })); } } } return(Task.CompletedTask); } }
/// <summary> /// Sets up some default values for the various settings. /// User code should use EngineSettings.Current to access the singleton instead of constructing a new object. /// </summary> public EngineSettings() { SessionLogPath = new MacroString { Text = "SessionLogs/SessionLog <Date>.txt" }; ResultLatencyLimit = 3.0; OperatorName = Environment.GetEnvironmentVariable("USERNAME") ?? Environment.GetEnvironmentVariable("USER") ?? ""; StationName = Environment.MachineName; // Set OpenTAP to abort on step error by default. AbortTestPlan = AbortTestPlanType.Step_Error; PromptForMetaData = false; ResourceManagerType = new ResourceTaskManager(); }
async Task OpenResource(LazyResourceManager requester, CancellationToken cancellationToken) { var node = ResourceNode; foreach (var dep in node.StrongDependencies) { if (dep == null) { continue; } await requester.RequestResourceOpen(dep, cancellationToken); } Stopwatch swatch = Stopwatch.StartNew(); try { // start a new thread to do synchronous work await Task.Factory.StartNew(node.Resource.Open); var reslog = ResourceTaskManager.GetLogSource(node.Resource); reslog.Info(swatch, "Resource \"{0}\" opened.", node.Resource); } finally { lock (LockObj) if (state == ResourceState.Opening) { state = ResourceState.Open; } } foreach (var dep in node.WeakDependencies) { if (dep == null) { continue; } await requester.RequestResourceOpen(dep, cancellationToken); } requester.ResourceOpenedCallback(node.Resource); }