Monday 6 July 2009

Compare-Int32.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script illustrates the CompareExchange Method 
  4. .DESCRIPTION 
  5.     This script creates three values, and calls CompareExchange method, 
  6.     and displays the results. The first time, we compare two non-equal values 
  7.     so no exchange is done. The second time, the comparison succeeds and the 
  8.     value us updated.    
  9. .NOTES 
  10.     File Name  : Compare-Int32.ps1 
  11.     Author     : Thomas Lee - tfl@psp.co.uk 
  12.     Requires   : PowerShell V2 CTP3 
  13. .LINK 
  14.     This script posted to: 
  15.         http://www.pshscripts.blogspot.com 
  16.     MSDN Sample posted at: 
  17.         http://msdn.microsoft.com/en-us/library/801kt583.aspx 
  18. .EXAMPLE 
  19.     PSH [C:\foo]: .\Compare-int32.PS1 
  20.     Before 1st call: 
  21.     42 
  22.     69 
  23.     104 
  24.   
  25.     After 1st call, before 2nd 
  26.     42 
  27.     69 
  28.     104 
  29.   
  30.     After 2nd call 
  31.     69 
  32.     69 
  33.     104 
  34. #> 
  35.  
  36. ## 
  37. # Start of Script 
  38. ## 
  39.  
  40. # Create 3 int32 values 
  41.  
  42. [int32] $a=42 
  43. [int32] $b=69 
  44. [int32] $c=104 
  45.  
  46. # Display values before 
  47. "Before 1st call:";$a,$b,$c 
  48. "" 
  49.  
  50. # Call CompareExchange and print results 
  51. # This call compares $a with $c, so there are not equal 
  52. $result = [System.Threading.Interlocked]::CompareExchange([ref]$a, $b, $c
  53. "After 1st call, before 2nd" 
  54. $a,$b,$c 
  55. "" 
  56.  
  57. # Call CompareExchange and print results 
  58. # This call compares $a with $A, so this call 
  59. # returns $a updated to $b 
  60. $result = [System.Threading.Interlocked]::CompareExchange([ref]$a, $b, $a
  61. "After 2nd call" 
  62. $a,$b,$c 

No comments: