Discussion:
Is the event handler limited under PowerShell v2 ?
(too old to reply)
BatchMan
2010-05-27 13:56:58 UTC
Permalink
Hi
this script do not work (BindingList.AddingNew Event)

Code
-------------------

#The PSMemberSet class has not a default constructor
$listOfParts = new-object System.ComponentModel.BindingList[System.Management.Automation.PSMemberSet

$listOfParts.AllowNew = $tru
$listOfParts.AllowRemove = $Tru
$listOfParts.AllowEdit = $True
# Raise ListChanged events when new parts are added
$listOfParts.RaiseListChangedEvents = $tru

Register-ObjectEvent $listOfParts AddingNew "AddingNew" -Action
Write-Warning "args count";
Write-host $event.SourceArgs.Coun
$EventArgs|select * |write-host
$obj=New-object System.Management.Automation.PSMemberSet "Test1
#$EventArgs.NewObject=$Ob
$Event.SourceArgs[1].NewObject=$Ob


Register-ObjectEvent $listOfParts ListChanged "ListChanged" -Action
Write-WarnIng "ListChanged
$EventArgs|select * |write-hos
Write-Host $EventArgs.ListChangedType.ToString(


$Newpart=$listOfParts.Add((New-object System.Management.Automation.PSMemberSet "Test0")

$Newpart=$listOfParts.AddNew(
#exceptio
# 'BindingList.AddNew Method (System.ComponentModel)' (http://msdn.microsoft.com/en-us/library/ms132687(v=VS.80).aspx
$listOfParts.EndNew($listOfParts.Count

-------------------

It is a limit or a bug

--
BatchMan
Bob Landau
2010-06-01 00:43:01 UTC
Permalink
BatchMan,

I need to preface this by saying I've never heard much less used this class.
Having said that I suspect that this may be a bug however you need to clean
up your code. As it is there is no progressive set of statements that would
tell me where of if there was a bug.

The place to report a bug is https://connect.microsoft.com. Here is what
I've found


$listOfInts = New-Object System.ComponentModel.BindingList[int]

$job = Register-ObjectEvent -InputObject $listOfInts -EventName AddingNew
-Action { $args.count }

$listOfInts.AddNew()

Get-Job $job.id
Receive-Job $job.id ## reports 2
Unregister-Event $job.id


$job = Register-ObjectEvent -InputObject $listOfInts -EventName AddingNew
-Action { $args[0].gettype(); $args[1].gettype() }

$listOfInts.AddNew()
Get-Job $job.id
Receive-Job $job.id ## reports there type one of which is AddingNewEventArgs
Unregister-Event $job.id


$job = Register-ObjectEvent -InputObject $listOfInts -EventName AddingNew
-Action { $args[1].NewEvent = 42 }
$listOfInts.AddNew()
Get-Job $job.id
Receive-Job $job.id ## no joy
Unregister-Event $job.id
$listOfInts ## last value should have been the answer to the universe

$job = Register-ObjectEvent -InputObject $listOfInts -EventName AddingNew
-Action { $args[1].NewEvent -eq $null }
$listOfInts.AddNew()
Get-Job $job.id
Receive-Job $job.id ## reports true << this I believe is unexepected.

When you submit the bug keep it precise and is possible which is in your
case provide a simple equivalent "working" sample in C#.

BTW I am in no way stating I work for Microsoft. All I'm saying is when a
bug gets reported to me the easier it is to understand the more likely it
will be resolved in your favor.

bob
Hi,
--------------------
#The PSMemberSet class has not a default constructor
$listOfParts = new-object System.ComponentModel.BindingList[System.Management.Automation.PSMemberSet]
$listOfParts.AllowNew = $true
$listOfParts.AllowRemove = $True
$listOfParts.AllowEdit = $True;
# Raise ListChanged events when new parts are added.
$listOfParts.RaiseListChangedEvents = $true
Register-ObjectEvent $listOfParts AddingNew "AddingNew" -Action {
Write-Warning "args count";
Write-host $event.SourceArgs.Count
$EventArgs|select * |write-host
$obj=New-object System.Management.Automation.PSMemberSet "Test1"
#$EventArgs.NewObject=$Obj
$Event.SourceArgs[1].NewObject=$Obj
}
Register-ObjectEvent $listOfParts ListChanged "ListChanged" -Action {
Write-WarnIng "ListChanged"
$EventArgs|select * |write-host
Write-Host $EventArgs.ListChangedType.ToString()
}
$Newpart=$listOfParts.Add((New-object System.Management.Automation.PSMemberSet "Test0"))
$Newpart=$listOfParts.AddNew()
#exception
# 'BindingList.AddNew Method (System.ComponentModel)' (http://msdn.microsoft.com/en-us/library/ms132687(v=VS.80).aspx)
$listOfParts.EndNew($listOfParts.Count)
--------------------
It is a limit or a bug ?
--
BatchMan
.
Loading...