public void Close() { try { if (Agent.AgentType == Agent.eAgentType.Service) { if (gingerNodeInfo != null) { // this is plugin driver on local machine GingerNodeProxy.GingerGrid = WorkSpace.Instance.LocalGingerGrid; GingerNodeProxy.CloseDriver(); gingerNodeInfo.Status = GingerNodeInfo.eStatus.Ready; if (mProcess != null) // it means a new plugin process was started for this agent - so we close it { // Remove the node from the grid WorkSpace.Instance.LocalGingerGrid.NodeList.Remove(gingerNodeInfo); // Close the plugin process mProcess.CloseMainWindow(); } GingerNodeProxy = null; gingerNodeInfo = null; return; } else { if (GingerNodeProxy != null) { // Running on Remote Grid GingerNodeProxy.CloseDriver(); GingerNodeProxy.Disconnect(); GingerNodeProxy = null; } } } if (Driver == null) { return; } Driver.IsDriverRunning = false; if (Driver is IDriverWindow && ((IDriverWindow)Driver).ShowWindow) { DriversWindowUtils.OnDriverWindowEvent(DriverWindowEventArgs.eEventType.CloseDriverWindow, Driver, this); Driver.CloseDriver(); } else if (Driver.Dispatcher != null) { Driver.Dispatcher.Invoke(() => { Driver.CloseDriver(); Thread.Sleep(1000); }); } else { Driver.CloseDriver(); } if (MSTATask != null) { // Using cancellation token source to cancel CancelTask = new BackgroundWorker(); CancelTask.DoWork += new DoWorkEventHandler(Agent.CancelTMSTATask); CancelTask.RunWorkerAsync(); } Driver = null; } finally { Agent.OnPropertyChanged(nameof(AgentOperations.Status)); Agent.OnPropertyChanged(nameof(AgentOperations.IsWindowExplorerSupportReady)); } }
public void StartDriver() { WorkSpace.Instance.Telemetry.Add("startagent", new { AgentType = Agent.AgentType.ToString(), DriverType = Agent.DriverType.ToString() }); if (Agent.AgentType == Agent.eAgentType.Service) { StartPluginService(); GingerNodeProxy.StartDriver(); Agent.OnPropertyChanged(nameof(Agent.Status)); } else { try { Agent.mIsStarting = true; Agent.OnPropertyChanged(nameof(Agent.Status)); try { if (Agent.Remote) { throw new Exception("Remote is Obsolete, use GingerGrid"); } else { Driver = (DriverBase)TargetFrameworkHelper.Helper.GetDriverObject(Agent); } } catch (Exception e) { Reporter.ToLog(eLogLevel.ERROR, "Failed to set Agent Driver", e); return; } if (Agent.AgentType == Agent.eAgentType.Service) { throw new Exception("Error - Agent type is service and trying to launch from Ginger.exe"); // we should never get here with service } else { Driver.InitDriver(Agent); Driver.BusinessFlow = Agent.BusinessFlow; SetDriverConfiguration(); IVirtualDriver VirtualDriver = null; if (Driver is IVirtualDriver VD) { VirtualDriver = VD; string ErrorMessage; if (!VirtualDriver.CanStartAnotherInstance(out ErrorMessage)) { throw new NotSupportedException(ErrorMessage); } } //if STA we need to start it on seperate thread, so UI/Window can be refreshed: Like IB, Mobile, Unix if (Driver is IDriverWindow && ((IDriverWindow)Driver).ShowWindow) { DriversWindowUtils.OnDriverWindowEvent(DriverWindowEventArgs.eEventType.ShowDriverWindow, Driver, this); Driver.StartDriver(); } else if (Driver.IsSTAThread()) { CTS = new CancellationTokenSource(); MSTATask = new Task(() => { Driver.StartDriver(); } , CTS.Token, TaskCreationOptions.LongRunning); MSTATask.Start(); } else { Driver.StartDriver(); } if (VirtualDriver != null) { VirtualDriver.DriverStarted(Agent.Guid.ToString()); } } } finally { if (Agent.AgentType == Agent.eAgentType.Service) { Agent.mIsStarting = false; } else { if (Driver != null) { // Give the driver time to start Thread.Sleep(500); Driver.IsDriverRunning = true; Driver.DriverMessageEvent += driverMessageEventHandler; } Agent.mIsStarting = false; Agent.OnPropertyChanged(nameof(Agent.Status)); Agent.OnPropertyChanged(nameof(IsWindowExplorerSupportReady)); } } } }