locked
Trying to retrieve value of a text box control RRS feed

  • Question

  • I am trying to create a multi threaded windows form but it seems to freeze on me when I try to retrieve the "text" property of a textbox control. I tried to do a .invoke on the textbox control but that didn't seem to work. maybe I was doing it wrong. here is what I got:

    $syncedhashtable = [hashtable]::synchronized(@{})

    add-type -assemblyname system.windows.forms,system.drawing $loginform = new-object system.windows.forms.form $textbox = new-object system.windows.forms.textbox $button = new-object system.windows.forms.button $button.location = new-object system.drawing.point 50,50 $button.add_click({$synchedhashtable.textboxvalue = $textbox.text}) $loginform.controls.addrange(@($textbox,$button)) $runspace = [runspacefactory]::createrunspace() $runspace.open() $runspace.sessionstateproxy.setvariable("loginform",$loginform) $pipeline = $runspace.createpipeline({$loginform.showdialog() | out-null}) $pipeline.input.close $pipeline.invokeasync()

    This will hang after pressing the button. I thought it had something to do with thread safe calling so I tried to do something like this...

    $button.add_click({$textbox.invoke({$syncedhashtable.textboxvalue = $textbox.text})})

    with the same result. I also tried grabbing the object from the parent like this...

    $button.add_click({$synchedhashtable.textboxvalue = $this.parent.controls["textbox"].text})
    this has the same result. Any help would be greatly appreciated. Thank you

    Monday, March 14, 2016 10:58 PM

Answers

  • This is one way to set it up:

    $code={
    	add-type -assemblyname system.windows.forms
    	$loginform = new-object system.windows.forms.form
    	$textbox = new-object system.windows.forms.textbox
    	$button = new-object system.windows.forms.button
    	$button.location = '50,50'
    	$button.add_click({$hash.Result = $textbox.text;$loginform.Close()})
    	$loginform.controls.addrange(@($textbox,$button))
    	$loginform.showdialog()
    }
    
    $hash = [hashtable]::synchronized(@{})
    $hash.Result=''
    $runspace = [runspacefactory]::createrunspace()
    $runspace.open()
    
    $runspace.sessionstateproxy.setvariable('hash',$hash)
    $pipeline = $runspace.createpipeline($code)
    $pipeline.input.close()
    $pipeline.invokeasync()
    # post the following line after the form has closed.
    $hash.Result


    \_(ツ)_/

    • Marked as answer by Ivory Mischke Tuesday, March 15, 2016 6:59 PM
    Monday, March 14, 2016 11:38 PM

All replies

  • You cannot run a form async.  You can run  XAML forms in a separate runspace.

    add-type -assemblyname system.windows.forms
    $loginform = new-object system.windows.forms.form
    $textbox = new-object system.windows.forms.textbox
    $button = new-object system.windows.forms.button
    $button.location = '50,50'
    $button.DialogResult='Ok'
    $loginform.controls.addrange(@($textbox,$button))
    $loginForm.ShowDiaqlog()
    $textbox.text
    


    \_(ツ)_/

    Monday, March 14, 2016 11:14 PM
  • This is one way to set it up:

    $code={
    	add-type -assemblyname system.windows.forms
    	$loginform = new-object system.windows.forms.form
    	$textbox = new-object system.windows.forms.textbox
    	$button = new-object system.windows.forms.button
    	$button.location = '50,50'
    	$button.add_click({$hash.Result = $textbox.text;$loginform.Close()})
    	$loginform.controls.addrange(@($textbox,$button))
    	$loginform.showdialog()
    }
    
    $hash = [hashtable]::synchronized(@{})
    $hash.Result=''
    $runspace = [runspacefactory]::createrunspace()
    $runspace.open()
    
    $runspace.sessionstateproxy.setvariable('hash',$hash)
    $pipeline = $runspace.createpipeline($code)
    $pipeline.input.close()
    $pipeline.invokeasync()
    # post the following line after the form has closed.
    $hash.Result


    \_(ツ)_/

    • Marked as answer by Ivory Mischke Tuesday, March 15, 2016 6:59 PM
    Monday, March 14, 2016 11:38 PM
  • Here is a demo of the async nature of the pseudo async form.

    $code={
    	add-type -assemblyname system.windows.forms
    	$loginform = new-object system.windows.forms.form
    	$textbox = new-object system.windows.forms.textbox
    	$button = new-object system.windows.forms.button
    	$button.location = '50,50'
    	$button.add_click({$hash.Result = $textbox.text})
    	$loginform.controls.addrange(@($textbox,$button))
    	$loginform.showdialog()
    }
    
    $hash = [hashtable]::synchronized(@{})
    $hash.Result=''
    $runspace = [runspacefactory]::createrunspace()
    $runspace.open()
    
    $runspace.sessionstateproxy.setvariable('hash',$hash)
    $pipeline = $runspace.createpipeline($code)
    $pipeline.input.close()
    $pipeline.invokeasync()
    # post the following line after the form has closed.
    
    $old=''
    while(1){
    	if($hash.Result -ne $old){
    		$old=$hash.result
    		Write-Host $old -fore green
    	}
    	sleep 1
    }
    
    


    \_(ツ)_/

    Monday, March 14, 2016 11:45 PM
  • Sending messages to the console async.

    $code={
    	add-type -assemblyname system.windows.forms
    	$loginform = new-object system.windows.forms.form
    	$textbox = new-object system.windows.forms.textbox
    	$button = new-object system.windows.forms.button
    	$button.location = '50,50'
    	$button.add_click({$hash.host.Ui.WriteVerboseLine($textbox.Text)})
    	$loginform.controls.addrange(@($textbox,$button))
    	$loginform.showdialog()
    }
    
    $hash = [hashtable]::synchronized(@{})
    $hash.Result=''
    $hash.host=$host
    $runspace = [runspacefactory]::createrunspace()
    $runspace.open()
    
    $runspace.sessionstateproxy.setvariable('hash',$hash)
    $pipeline = $runspace.createpipeline($code)
    $pipeline.input.close()
    $pipeline.invokeasync()
    
    
    



    \_(ツ)_/


    • Edited by jrv Monday, March 14, 2016 11:53 PM
    Monday, March 14, 2016 11:51 PM
  • Thanks for the super quick response. I wasn't able to test it until now but it worked perfectly. How do you know so much? It's amazing. Thanks for the help yet again!
    Tuesday, March 15, 2016 7:00 PM