private async Task ExecuteScript(RemoteTarget target, string script, Action <EventHandler> unsubscribeClosingEvent) { try { await target.EnterSessionExecute(script, _cancelTokenSource.Token); } finally { unsubscribeClosingEvent(_closingEventHandler); } }
/// <summary> /// Start the session and start the remote tool. /// This starts a backgroud task and leave it running till it is cancelled or stopped. /// </summary> /// <param name="computerName">The ipaddress of debugging target machine.</param> /// <param name="username">The username for authentication.</param> /// <param name="password">The password for authentication.</param> /// <param name="subscribeClosingEvent">The method to subscribe to solution closing event.</param> /// <param name="unsubscribeClosingEvent">The method to unsubscribe solution closing event.</param> public RemoteToolSession( string computerName, string username, string password, Action <EventHandler> subscribeClosingEvent, Action <EventHandler> unsubscribeClosingEvent) { var target = new RemoteTarget(computerName, RemotePowerShellUtils.CreatePSCredential(username, password)); string script = RemotePowerShellUtils.GetEmbeddedFile(StartPsFilePath); _closingEventHandler = (se, e) => Stop(); subscribeClosingEvent(_closingEventHandler); _powerShellTask = Task.Run(() => { try { target.EnterSessionExecute(script, _cancelTokenSource.Token); } finally { unsubscribeClosingEvent(_closingEventHandler); } }); }