/********************************************************************++
Copyright (c) Microsoft Corporation. All rights reserved.
--********************************************************************/
#region Using directives
using System;
using System.Management.Automation;
using System.Management.Automation.Internal;
#endregion
namespace Microsoft.PowerShell.Commands
{
///
/// Implements a cmdlet that applies a script block
/// to each element of the pipeline.
///
[Cmdlet(VerbsDiagnostic.Measure, "Command", HelpUri = "http://go.microsoft.com/fwlink/?LinkID=113348", RemotingCapability = RemotingCapability.None)]
[OutputType(typeof(TimeSpan))]
public sealed class MeasureCommandCommand : PSCmdlet
{
#region parameters
///
/// This parameter specifies the current pipeline object
///
[Parameter(ValueFromPipeline = true)]
public PSObject InputObject { set; get; } = AutomationNull.Value;
///
/// The script block to apply
///
[Parameter(Position = 0, Mandatory = true)]
public ScriptBlock Expression { set; get; }
#endregion
#region private members
private System.Diagnostics.Stopwatch _stopWatch = new System.Diagnostics.Stopwatch();
#endregion
#region methods
///
/// Output the timer
///
protected override void EndProcessing()
{
WriteObject(_stopWatch.Elapsed);
} // EndProcessing
///
/// Execute the script block passing in the current pipeline object as
/// it's only parameter.
///
protected override void ProcessRecord()
{
// Only accumulate the time used by this scriptblock...
// As results are discarded, write directly to a null pipe instead of accumulating.
_stopWatch.Start();
Expression.InvokeWithPipe(
useLocalScope: false,
errorHandlingBehavior: ScriptBlock.ErrorHandlingBehavior.WriteToCurrentErrorPipe,
dollarUnder: InputObject, // $_
input: new object[0], // $input
scriptThis: AutomationNull.Value,
outputPipe: new Pipe { NullPipe = true },
invocationInfo: null);
_stopWatch.Stop();
}
#endregion
}
} // namespace Microsoft.PowerShell.Commands