OK.  With the help of all your resources and another I found, 
https://smsagent.wordpress.com/2015/09/07/powershell-tip-utilizing-runspaces-for-responsive-wpf-gui-applications,
 I was able to get a working application that is in its own runspace and was 
able to get a function running in its own runspace and writing to a common gui 
textbox using .appendtext.  it took some doing and I had to re do the bit that 
uses .selectnode for the xml parsing to get all the variables right and get 
them into a synced hashtable, but I got it working and so far so good.

Writing to the textbox from the runspaced function required wrapping the whole 
write action with the .window.dispatcher.invoke({}) stuff and I had to add the 
.scrolltoend() method to get the window to keep scrolling to the bottom so the 
latest written there would stay visible.  Kinda gross looking and a lot to just 
write to a text box, but it works and produces the desired effect.

$syncHash.Window.Dispatcher.invoke([action]{
       
$syncHash.textBoxOutput.AppendText(('======================================================'
 +"`r`n"))
       $syncHash.textBoxOutput.AppendText(('Beginning operations.' +"`r`n"))
       $syncHash.textBoxOutput.ScrollToEnd()
})

On to my stumbling block…

My GUI has a couple of tabs in a tab control.  In Forms I could use the 
.add_click() method on the tabcontrol element and control elements of the UI 
depending on what tab got clicked on.  Like changing the text on the submit 
button.  (each tab does a different thing) but it seems that I can’t use 
.add_click() on a WPF tabcontrol control.  So how might I change the text of a 
button based on what tab the user clicks on, in WPF?  In forms it was easy, I 
could do nothing but click on a tab and watch the text on the button change 
from ‘Load’ to ‘Start’ and back again:

$tabcontrol1.add_Click( {
              If($tabcontrol1.SelectedIndex -eq '0')
              {
                     $buttonsubmit.Text = 'Load'
              }
              If($tabcontrol1.SelectedIndex -eq '1')
              {
                     $buttonsubmit.Text = 'Start'
                     $buttonsubmit.Enabled = $True
              }
       } )

The adjusted code for wpf which is basically the same, just with different 
variables, fails with

$syncHash.tabControl.add_click({
                     If($syncHash.tabControl.SelectedIndex -eq '0')
                     {
                           $syncHash.buttonSubmit.Content = 'Load'
                     }
                     If($syncHash.tabControl.SelectedIndex -eq '1')
                     {
                           $syncHash.buttonSubmit.IsEnabled = $True
                           $syncHash.buttonSubmit.Content = 'Start'
                     }
       })

Method invocation failed because [System.Windows.Controls.TabControl] does not 
contain a method named 'Add_Click'.

How can I make it run the check if I don’t have a click action on the tab 
control?

This is a small thing, and I have lots more code to adapt to runspaces, but 
this will likely rear its head again, one of the tabs has elements on it that 
hide, show, and bringtofront other elements depending on actions and choices in 
others.

Any ideas?  I may just not know enough.

Todd


From: [email protected] [mailto:[email protected]] On 
Behalf Of Mote, Todd
Sent: Friday, February 26, 2016 2:51 PM
To: '[email protected]' <[email protected]>
Subject: RE: [powershell] GUIs and runspaces

Huh, .appendtext() is what I’m using now all over my original script, and the 
ISE intellesenses it and auto completes it for me.  This way it doesn’t auto 
scroll all the way to the top of the box to replace all of the text, once I 
fill up the text box unless I figure out threading I won’t be able to scroll 
past the size of my box?




From: [email protected]<mailto:[email protected]> 
[mailto:[email protected]] On Behalf Of Ryan
Sent: Friday, February 26, 2016 12:32 PM
To: [email protected]<mailto:[email protected]>
Subject: Re: [powershell] GUIs and runspaces

You can pretty much lift all the original code. Microsoft tried to make the WPF 
names match the WinForms names so this could be done.

There is no appendtext method on text boxes. I would simply do $TextBox.Text = 
$TextBox.Text + "New text"

Bindings aren't really needed for text boxes and things like that. They are 
required for DataGrids (though it could happen in the background without you 
knowing) but I did a lot of work in WPF without doing any bindings. Simply 
interact directly with the controls (like I did above for the text box). 
Bindings greatly help the process along when you thread, but it's not a big 
deal if you don't.
On Fri, Feb 26, 2016 at 1:11 AM Mote, Todd 
<[email protected]<mailto:[email protected]>> wrote:
Ok, Ok, So I installed VS Community, recreated my UI, and got it running from 
ISE.  What took about 1000 lines to build in Forms only took about 75 lines in 
XAML.  You convinced me.

I had an issue with the select node code, but after messing around with it I 
ended up taking “[@Name]” out since you’re getting the $_.name property in the 
foreach.  So mine works looking like this:

$xaml.SelectNodes('//*') | Foreach-Object { Set-Variable -Name ('Window' + '_' 
+ $_.Name) -Value $Window.FindName($_.Name) }

Which is fine, it makes all the variables like I expect them to.  With the 
[@Name] in there it didn’t return any results.

Got my “Cancel” button closing the form too, and populated some of my pull 
downs.  Now the real work begins…

I can pretty much lift all the functions and actions for all the buttons and 
pulldown elements from my original code to this, changing the names of the 
variables of course, right?  Basically I can get it working the same way it’s 
working now, just with WPF instead of forms?

With all the stuff I do, I have a text box for output when users select things 
in pull downs and click buttons, how do I append text to my output text box if 
it’s in the XAML?  Or is that the same too, I just do 
$window_textboxOutput.appendtext((“stuff”))?  It didn’t seem to work.

I do want to runspace some things.  I pretty sure I will have to do the binding 
bit you talked about if I thread a button action that writes output to the 
output text box, right?

Fantastic series of post too, thanks much for writing them.  I’m excitd to try 
threading stuff, but I should probably get a handle on WPF I guess first..

Thanks Ryan, and if at any point this is too much for the list I’m happy to 
take it offlist if you have the time.

From: [email protected]<mailto:[email protected]> 
[mailto:[email protected]<mailto:[email protected]>] 
On Behalf Of Mote, Todd
Sent: Thursday, February 25, 2016 7:19 PM
To: [email protected]<mailto:[email protected]>
Subject: RE: [powershell] GUIs and runspaces

Windows forms, by hand.  (I really need a copy of visual studio or powershell 
studio, or something..  ☺ )

Thanks so much guys.  I had totally forgotten about this list until I saw the 
error handling thread.  I’d found http://poshcode.org/5520 and got some of that 
to work, partly.  I’ll start looking through these in earnest, but it looks 
like exactly what I need.  my ui is pretty complicated and lots of elements do 
or don’t do things depending on data in other elements, so I’m not sure how it 
will go, but I’ll give it a try.

Xaml looks somewhat simpler to write, but I’ll save that for another day.  ☺




From: [email protected]<mailto:[email protected]> 
[mailto:[email protected]] On Behalf Of Ryan
Sent: Thursday, February 25, 2016 5:04 PM
To: [email protected]<mailto:[email protected]>
Subject: Re: [powershell] GUIs and runspaces

Is this WPF or Windows Forms?

When I started writing WinForms, this blog post helped me learn about different 
runspaces and how to make the forms more responsive:  
https://www.sapien.com/blog/2012/05/16/powershell-studio-creating-responsive-forms/

If you are doing it in WPF, I have a series of posts on UI development: 
http://www.ephingadmin.com/better-know-a-powershell-ui/
The "Quack like a duck" post talks about different threads and uses PoshRSJob 
(3rd party cmdlet on GitHub) to do the threading.

Boe Prox (the author of PoshRSJob) has a lot of good information on this also. 
Here's a post about writing data from one thread to another:
http://learn-powershell.net/2012/10/14/powershell-and-wpf-writing-data-to-a-ui-from-a-different-runspace/

Let me know if you need any other resources!


On Thu, Feb 25, 2016 at 4:59 PM Mote, Todd 
<[email protected]<mailto:[email protected]>> wrote:
So I've written a fancy PowerShell script and given it a GUI and it works 
great.  As I add more features to it though, I find that it’s taking longer and 
longer to run and thought I would try to get it to run in a separate thread to 
free up the GUI.  I’m not finding many resources to help me learn how to do 
that. Can anybody point me to any?  I know about sunspaces, bur have not had 
any luck with getting them to work.  Help?

Sent from my Windows 10 phone


================================================
Did you know you can also post and find answers on PowerShell in the forums?
http://www.myitforum.com/forums/default.asp?catApp=1

================================================
Did you know you can also post and find answers on PowerShell in the forums?
http://www.myitforum.com/forums/default.asp?catApp=1

================================================
Did you know you can also post and find answers on PowerShell in the forums?
http://www.myitforum.com/forums/default.asp?catApp=1

================================================
Did you know you can also post and find answers on PowerShell in the forums?
http://www.myitforum.com/forums/default.asp?catApp=1

================================================
Did you know you can also post and find answers on PowerShell in the forums?
http://www.myitforum.com/forums/default.asp?catApp=1

================================================
Did you know you can also post and find answers on PowerShell in the forums?
http://www.myitforum.com/forums/default.asp?catApp=1

================================================
Did you know you can also post and find answers on PowerShell in the forums?
http://www.myitforum.com/forums/default.asp?catApp=1

Reply via email to