//! Called by the job thread. void AsyncInvoke(IAsyncResult ar) { // end; it may throw, e.g. if stopped try { PowerShell.EndInvoke(ar); } catch (RuntimeException) { } // state switch (PowerShell.InvocationStateInfo.State) { case PSInvocationState.Completed: if (IsHidden) { // OK: discard if (PowerShell.Streams.Error.Count == 0) { Dispose(); return; } // KO: make it UI JobUI = new JobUI(); JobList.Add(this); JobUI.HasError = true; A.WriteErrors(JobUI.GetWriter(), PowerShell.Streams.Error); } break; case PSInvocationState.Failed: // make UI for a hidden job, and (!) write not terminating errors first if (IsHidden) { JobUI = new JobUI(); JobList.Add(this); A.WriteErrors(JobUI.GetWriter(), PowerShell.Streams.Error); } // UI if (JobUI != null) { JobUI.HasError = true; A.WriteException(JobUI.GetWriter(), PowerShell.InvocationStateInfo.Reason); } break; } // UI if (JobUI != null) { // close not needed now UI JobUI.Close(); // post notificator Far.Api.PostJob(WatchJobs); } }
/// <summary> /// New job. /// </summary> /// <remarks> /// Keep seconds for UI-less jobs: 0 ~ hidden mode, in this case a job creates UI on errors, as it is not attended. /// Other UI-less jobs are completely owned creators. /// </remarks> internal Job(JobCommand command, object parameters, string name, bool ui, int keepSeconds) { JobCommand = command; Parameters = parameters; Name = name; KeepSeconds = keepSeconds; // create/open runspace //! *) Do not catch, if we fail, we fail and there is nothing to repair yet (not open) //! *) Use existing configuration, it is faster! Most of *-Far* cmdlets should not be used, //! but some of them can be used, e.g. Update-FarDescription; also we want to use ETS types, //! e.g. FarDescription property. if (ui) { JobUI = new JobUI(); Runspace = RunspaceFactory.CreateRunspace(new FarHost(JobUI), Runspace.DefaultRunspace.InitialSessionState); } else { //! DefaultHost is created internally. Perhaps it is reasonable to live with it, not with a custom host. Runspace = RunspaceFactory.CreateRunspace(Runspace.DefaultRunspace.InitialSessionState); } Runspace.Open(); // new shell with the command PowerShell = PowerShell.Create(); PowerShell.Runspace = Runspace; JobCommand.Add(PowerShell); // add command parameters if (parameters != null) { IDictionary namedParameters = parameters as IDictionary; IList argumentList; if (namedParameters != null) PowerShell.AddParameters(namedParameters); else if ((argumentList = parameters as IList) != null) PowerShell.AddParameters(argumentList); else PowerShell.AddParameters(new object[] { parameters }); } // UI: Write all output, including errors. if (JobUI != null) { PowerShell.Commands.AddCommand(A.OutHostCommand); } // Hidden: Write output to "Out-Null" to avoid memory use. else if (keepSeconds <= 0) { //! User can use his Out-Null PowerShell.AddCommand("Out-Null"); } // Output: create it once: it is cumulative else { Output = new PSDataCollection<PSObject>(); } }
/// <summary> /// New job. /// </summary> /// <remarks> /// Keep seconds for UI-less jobs: 0 ~ hidden mode, in this case a job creates UI on errors, as it is not attended. /// Other UI-less jobs are completely owned creators. /// </remarks> internal Job(JobCommand command, object parameters, string name, bool ui, int keepSeconds) { JobCommand = command; Parameters = parameters; Name = name; KeepSeconds = keepSeconds; // create/open runspace //! *) Do not catch, if we fail, we fail and there is nothing to repair yet (not open) //! *) Use existing configuration, it is faster! Most of *-Far* cmdlets should not be used, //! but some of them can be used, e.g. Update-FarDescription; also we want to use ETS types, //! e.g. FarDescription property. if (ui) { JobUI = new JobUI(); Runspace = RunspaceFactory.CreateRunspace(new FarHost(JobUI), Runspace.DefaultRunspace.InitialSessionState); } else { //! DefaultHost is created internally. Perhaps it is reasonable to live with it, not with a custom host. Runspace = RunspaceFactory.CreateRunspace(Runspace.DefaultRunspace.InitialSessionState); } Runspace.Open(); // new shell with the command PowerShell = PowerShell.Create(); PowerShell.Runspace = Runspace; JobCommand.Add(PowerShell); // add command parameters if (parameters != null) { IDictionary namedParameters = parameters as IDictionary; IList argumentList; if (namedParameters != null) { PowerShell.AddParameters(namedParameters); } else if ((argumentList = parameters as IList) != null) { PowerShell.AddParameters(argumentList); } else { PowerShell.AddParameters(new object[] { parameters }); } } // UI: Write all output, including errors. if (JobUI != null) { PowerShell.Commands.AddCommand(A.OutHostCommand); } // Hidden: Write output to "Out-Null" to avoid memory use. else if (keepSeconds <= 0) { //! User can use his Out-Null PowerShell.AddCommand("Out-Null"); } // Output: create it once: it is cumulative else { Output = new PSDataCollection <PSObject>(); } }