2/6/10

PowerShell Asynchronous Background Jobs

PowerShell Background Jobs - MSDN.
Requires PowerShell v2.

Job Cmdlets
start-job
get-job
receive-job
stop-job
start-job {
  #...
}

#with parameters
start-job {param($p1, $p2)
  $p1
  $p2
} -argumentList 'foo', 123

#output
foo
123

#execute N times
start-job {
  1..3 |
    %{
        (get-date).toString('HH:mm:ss')
        sleep 1
     }
}

#output
20:32:51
20:32:52
20:32:53

#execute N times with script block begin-process-end clause
start-job {
  begin
  {
    'begin script...'
  }
  process
  {
    1..3 | %{(get-date).toString('HH:mm:ss'); sleep 1;}
  }
  end
  {
    'end script...'
  }
}

#output
begin script...
20:35:42
20:35:43
20:35:44
end script...

#infinite loop, stop job during sleep interval
start-job {param($sleep)
  while ($true)
  {
    #...

    $now = get-date
    'sleep {0}, {1} - {2}' -f $sleep, $now.toString('HH:mm:ss'), $now.addSeconds($sleep).toString('HH:mm:ss')
    sleep $sleep
  }
} -argumentList 60

#output
sleep 60, 20:41:06 - 20:42:06
sleep 60, 20:42:06 - 20:43:06

2/5/10

Generic Array to String Function

ArrayToString returns an array as a formatted string. This generic function works with arrays of all types.
//C#
using System.Text;

namespace ExpressionSoftware.System
{
    public static class Array
    {
        public static string ArrayToString<T>(T[] array, string format)
        {
            return ArrayToString(array, format, 16);
        }

        public static string ArrayToString<T>(T[] array, string format, int stringBuilderItemCapacity)
        {
            var sb = new StringBuilder(array.Length * stringBuilderItemCapacity);
            foreach (T item in array)
            {
                sb.AppendFormat(format, item);
            }
            return sb.ToString();
        }
    }
}

//F# v1.9.7.8
namespace ExpressionSoftware.System
open System.Text

type Array =
  
  //stringBuilderItemCapacity is an optional int parameter
  static member ArrayToString(array:'a[], format:string, ?stringBuilderItemCapacity:int) =
    
    let stringBuilderItemCapacity =
      match stringBuilderItemCapacity with
        | Some(stringBuilderItemCapacity) -> stringBuilderItemCapacity
        | None -> 16

    let sb = new StringBuilder(array.Length * stringBuilderItemCapacity)
    array |> Array.iter(fun b -> sb.AppendFormat(format, b) |> ignore)
    sb.ToString()

#PowerShell
function arrayToString($array, $format)
{
  $array | %{$result += ($format -f $_)}
  $result
}

Examples
//C#
byte[] bytes = { 0, 1, 255 };
int[] ints = { int.MinValue, -1, 0, 256, int.MaxValue };
float[] floats = { -9.99f, 0f, 3.14159f };
char[] chars = { 'a', 'b', 'c', '1', '2', '3' };
string[] strings = { "xyz", "789" };

Debug.WriteLine(Array.ArrayToString(bytes, "{0}", 3));
Debug.WriteLine(Array.ArrayToString(bytes, "{0} "));
Debug.WriteLine(Array.ArrayToString(ints, "{0} "));
Debug.WriteLine(Array.ArrayToString(floats, "{0} "));
Debug.WriteLine(Array.ArrayToString(chars, "{0}", 1));
Debug.WriteLine(Array.ArrayToString(chars, "{0},", 2));
Debug.WriteLine(Array.ArrayToString(strings, "{0} ", 4));

//F#
let bytes = [|0uy; 1uy; 255uy|]
let ints = [|Int32.MinValue; -1; 0; 256; Int32.MaxValue|]
let floats = [|-9.99f; 0.0f; 3.14159f|]
let chars = [|'a'; 'b'; 'c'; '1'; '2'; '3'|]
let strings = [|"xyz"; "789"|]

Array.ArrayToString(bytes, "{0}", 3) |> (fun s -> printfn "%s" s)
Array.ArrayToString(bytes, "{0} ") |> (fun s -> printfn "%s" s)
Array.ArrayToString(ints, "{0} ") |> (fun s -> printfn "%s" s)
Array.ArrayToString(floats, "{0} ") |> (fun s -> printfn "%s" s)
Array.ArrayToString(chars, "{0}", 1) |> (fun s -> printfn "%s" s)
Array.ArrayToString(chars, "{0},", 2) |> (fun s -> printfn "%s" s)
Array.ArrayToString(strings, "{0} ", 4) |> (fun s -> printfn "%s" s)

#PowerShell
[byte[]]$bytes = 0, 1, 255
[int[]] $ints = [int32]::MinValue, -1, 0, 256, [int32]::MaxValue
[single[]] $floats = -9.99, 0, 3.14159
[char[]] $chars = 'a', 'b', 'c', '1', '2', '3'
[string[]] $strings = 'xyz', '789'

arrayToString $bytes '{0}'
arrayToString $bytes '{0} '
arrayToString $ints '{0} '
arrayToString $floats '{0} '
arrayToString $chars '{0}'
arrayToString $chars '{0},'
arrayToString $strings '{0} '

Output
01255
0 1 255 
-2147483648 -1 0 256 2147483647 
-9.99 0 3.14159 
abc123
a,b,c,1,2,3,
xyz 789