powershell - POSH: New-Event on Variable changed or filled -


i trigger event when variable filled / changed or when has defined value. 1 of these possible ?

i have barcode scanner connected serial running in different runspace. , created synchronised hashtable share barcode.

i want trigger event when hashtable.value contains barcode can add gui without blocking current thread.

(i know make system.timers.timer object en poll every 100ms) rather use event.)

without knowing how ui made up, example rather basic shows how can use observablecollection bind listbox auto-update listbox whenever added collection (even runspace).

$uihash = [hashtable]::synchronized(@{}) $uihash.observablecollection = $script:observablecollection = new-object system.collections.objectmodel.observablecollection[string] $runspacehash = [hashtable]::synchronized(@{}) $newrunspace =[runspacefactory]::createrunspace() $newrunspace.apartmentstate = "sta" $newrunspace.threadoptions = "reusethread"           $newrunspace.open() $newrunspace.sessionstateproxy.setvariable("uihash",$uihash)           $newrunspace.sessionstateproxy.setvariable("runspacehash",$runspacehash)  $pscmd = [powershell]::create().addscript({    #build gui [xml]$xaml = @" <window      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"     x:name="window" title="initial window" windowstartuplocation = "centerscreen"      width = "313" height = "800" showintaskbar = "true" background = "lightgray">      <scrollviewer verticalscrollbarvisibility="auto">         <stackpanel >             <textbox  isreadonly="true" textwrapping="wrap">                 type , click add             </textbox>             <textbox x:name = "inputbox"/>             <button x:name="button1" content="add"/>             <button x:name="button2" content="remove"/>             <expander isexpanded="true">                 <listbox x:name="listbox" selectionmode="extended" />             </expander >         </stackpanel>     </scrollviewer > </window> "@  $reader=(new-object system.xml.xmlnodereader $xaml) $uihash.window=[windows.markup.xamlreader]::load( $reader )   #connect controls $uihash.inputbox = $uihash.window.findname('inputbox') $uihash.button1 = $uihash.window.findname('button1') $uihash.button2 = $uihash.window.findname('button2') $uihash.listbox = $uihash.window.findname('listbox')  $uihash.window.add_sourceinitialized({     #have have in collection     $uihash.listbox.itemssource = $uihash.observablecollection     $uihash.inputbox.focus() })   #events $uihash.button1.add_click({      $uihash.observablecollection.add($uihash.inputbox.text)      $uihash.inputbox.clear() }) $uihash.button2.add_click({     foreach ($item in @($uihash.listbox.selecteditems)) {         $uihash.observablecollection.remove($item)     } })  $uihash.window.showdialog() | out-null }) $pscmd.runspace = $newrunspace $handle = $pscmd.begininvoke() 

with window open, can send data observablecollecton way of dispatcher on window. each time collection updated, listbox updated in same way (add, remove or clear).

# while form open, can send data form updating observablecollection # has done on ui thread though! $uihash.window.dispatcher.invoke('normal',[action]{     $uihash.observablecollection.add(1) }) 

code below added ensure gets disposed after form has been closed in example.

# once form has closed, make sure dispose of $pscmd.endinvoke($handle) 

Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -