Wednesday, 1 July 2009

Compare-Double

  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 to 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-Double.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/cd0811yf.aspx 
  18. .EXAMPLE 
  19.     PSH [C:\foo]: .\Compare-Double.PS1
  20.     Before 1st call: 
  21.     42.42 
  22.     69.69 
  23.     104.4 
  24.   
  25.     After 1st call, before 2nd 
  26.     42.42 
  27.     69.69 
  28.     104.4 
  29.   
  30.     After 2nd call 
  31.     69.69 
  32.     69.69 
  33.     104.4 
  34. #> 
  35.  
  36. ## 
  37. # Start of Script 
  38. ## 
  39.  
  40. # Create 3 double values 
  41.  
  42. [double] $a=42.42 
  43. [double] $b=69.69 
  44. [double] $c=104.4 
  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. # Call CompareExchange and print results 
  57. # This call compares $a with $A, so this call 
  58. # returns $a updated to $b 
  59. $result = [system.Threading.Interlocked]::CompareExchange([ref]$a, $b, $a
  60. "After 2nd call" 
  61. $a,$b,$c 

No comments: