How to reference variables while multi-threading in this program?

This topic contains 2 replies, has 2 voices, and was last updated by Profile photo of Poseyj Poseyj 5 months, 1 week ago.

  • Author
    Posts
  • #63787
    Profile photo of Poseyj
    Poseyj
    Participant

    I'm trying to test a simple GUI app in Powershell and I'm very new to GUIs and threading. I make the GUI in Visual Studio and copy the XAML to Powershell ISE. My ultimate goal is to have a form with text fields that are dynamic, where I type something in and it goes out to check on the internet to see if it's valid.(similar to how websites dynamically check email addresses to see if they're taken), but without hanging the GUI.

    From what I've read, namespaces have the least latency as opposed to Start-job, so I'm attempting to use one to accomplish the work in this, while the user can just tab on. I realize the problem is that in the namespace, it can't see the variables I need to manipulate. So my question is can someone tell me the best direction to go in order to achieve this? Code for this small program is below. Under MAIN, you'll see two commands I want to run. The first one works because it's not in the namespace, the second one fails because it can't see the variable.

    EDIT: it's not letting me post all the code. There's XAML at the top and a line that turns those gui objects into objects, the ones that start with WPF.

    $WPFTextBox_1.Add_LostFocus({

    $WPFLabel_1.Content = "Check 1" # This displays

    $code = {$WPFLabel_1.Content = "Check 2"} # This doesn't display
    $newPowerShell = [PowerShell]::Create().AddScript($code)
    $job = $newPowerShell.BeginInvoke()
    While (-Not $job.IsCompleted) {}
    $result = $newPowerShell.EndInvoke($job)
    $newPowerShell.Dispose()

    })

  • #63802
    Profile photo of Max Kozlov
    Max Kozlov
    Participant

    you need to organize external data exchange between different instances.

    try to check Boe Prox's blog at https://learn-powershell.net/
    somewhere he describe data exhange with synchronized hash and have alot of wpf samples to learn. btw may be the PoshRSJob module can help you

    • #63874
      Profile photo of Poseyj
      Poseyj
      Participant

      Very cool, thanks. Going to try out PoshRSJob

You must be logged in to reply to this topic.