Asynchronous Child forms

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 2 years and 1 week old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
gitpowershelluser
Posts: 24
Last visit: Mon Feb 06, 2023 11:30 am
Has voted: 1 time

Asynchronous Child forms

Post by gitpowershelluser »

I know we need to use runspaces to make childforms run so both forms can be "active"
I know we can parameters to the child form
I know we can use the closing event from the childform to pass info back to the mainform - I don't want this :)
I'm using a switch statement to my childform picturebox and textbox. The childform is a fancy dialogbox.

Issue 1 : How to do this with parameters to the child form?
Issue 2: How do I pass information back and forth between the two forms asynchronously?

When you open the main form, click the Help > App Support , then click send to understand more what I'm asking.
TempChildform.zip
(42.05 KiB) Downloaded 52 times
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Asynchronous Child forms

Post by jvierra »

Please see the following blog article on how to pas to child forms and retrieve child form data.

https://www.sapien.com/blog/2013/10/01/ ... ing-forms/
gitpowershelluser
Posts: 24
Last visit: Mon Feb 06, 2023 11:30 am
Has voted: 1 time

Re: Asynchronous Child forms

Post by gitpowershelluser »

Not what I'm looking for. More like this..

viewtopic.php?p=57595#p57595

I found a project you made ( Demo-MultFormClient ) and I changed some things
  1.  
  2. function Get-MsgBox
  3. {
  4.     param (
  5.         $Img,
  6.         $Msg
  7.     )
  8.    
  9.     $runspace = [runspacefactory]::createrunspace()
  10.     $runspace.open()
  11.     $synchash = [hashtable]::synchronized(@{
  12.             Form = $MainForm;
  13.             Img  = $Img;
  14.             Msg  = $Msg
  15.         })
  16.     $runspace.sessionstateproxy.setvariable('synchash', $synchash)
  17.     $code1 = (Get-Item function:'Show-ChildForm_psf').ScriptBlock
  18.     $pipeline = $runspace.createpipeline($code1)
  19.     $pipeline.invokeAsync()
  20. }
  21.  
Now on to my next problems

Creating a toolstrip of websites from a CSV dynamically
column 1 has the name of the website, column has the url
Have a click event open edge to the URL

as pages change or internal sites are added, the CSV needs to be hosted on the server
Snag_5a1e56d.png
Snag_5a1e56d.png (6.85 KiB) Viewed 1298 times

second,
Pass Set-ControlTheme to my childform
Snag_5ced545.png
Snag_5ced545.png (5.98 KiB) Viewed 1293 times
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Asynchronous Child forms

Post by jvierra »

If you have a new question the please follow normal forum rules to open new topic.

Please also note that forums are for asking coding questions and not for consulting services.

You stated in your first post that you wanted to use a project and not a Runspace. Note that Runspaces are tragically difficult even for experienced programmers.
gitpowershelluser
Posts: 24
Last visit: Mon Feb 06, 2023 11:30 am
Has voted: 1 time

Re: Asynchronous Child forms

Post by gitpowershelluser »

My apoligies for not using the terms incorrectly, please can we move forward?

One last question for Asynchronous Child forms if I may, since it falls under the umbrella

I'm using the function Set-ControlTheme in my Globals.PS1
Why isn't that being passed to my childform?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Asynchronous Child forms

Post by jvierra »

You cannot pass functions to a Runspace. As I warned you will need to learn about runspaces and runspace management to use a runspace for more than the simplest of things.

There are documents on MS and many blogs that discuss the various issues with runspaces but the issues get more challenging when you try to access a runspace from another runspace. WinForms runs in a runspace although it is just a simple thread of execution and is not managed as a runspace. A runspace is a wrapper around a thread to allow ease of management. "Ease" is more of a joke than anything although the runspace factory allows auto-creation of runspaces and global management of runspaces. All runspaces and the factory add layers of technical complexity.

Overall, you cannot pass a function to a runspace and it is exceptionally difficult to remotely execute code between runspaces to thread isolation. Best you find a way to use events in the form to execute the code.

I don't have a current link to any posts on runspaces. Search and you will find many blogs, but you will likely have to read 6 or more to find information that can help.
gitpowershelluser
Posts: 24
Last visit: Mon Feb 06, 2023 11:30 am
Has voted: 1 time

Re: Asynchronous Child forms

Post by gitpowershelluser »

Can’t pass a function - clear as mud

Currently, I can pass parameters & I can execute code between the forms via $synchash

I learn some by reading
I learn a lot by doing it all the wrong ways so I know how to do it effectively
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Asynchronous Child forms

Post by jvierra »

You said you were trying "Pass Set-ControlTheme to my childform". How can you "pass" a function? YOu can send objects to a runspace. YOu can add code to a runspace as global code. YOu still have to trigger something to execute the remote code. What you have posted so far is way too vague.

Reading is critical to all technology, science and engineering. Guessing will cause you to reinvent thousands of wheels and you may still never find the answer. Learning a technology from the basics requires reading. Most won't read because they lack enough of the basics to understand what they are reading. I recommend either searching blogs for clues or learning enough about coded systems to be able to ask better and more targeted questions. Remember that we can't see you screen or your code and even if we had the code, we would still have to understand what you mean by "Pass Set-ControlTheme to my childform".
User avatar
Alexander Riedel
Posts: 8479
Last visit: Thu Mar 28, 2024 9:29 am
Answers: 19
Been upvoted: 37 times

Re: Asynchronous Child forms

Post by Alexander Riedel »

Let's set the philosophy about learning by reading versus doing aside. Everyone learns differently.
James is right though, runspaces in PowerShell are not for the timid. They are also not design for GUI tasks, events, thread and whatnot, because that was not the intention when they were conceived.

I would submit that if you need any number of asynchronous forms, communicating with one another, you are far better off creating different processes and using whatever type of inter-process communication fits your needs best. This way you do not have to fiddle with runspaces, passing objects around and its its far easier to debug too.
Alexander Riedel
SAPIEN Technologies, Inc.
gitpowershelluser
Posts: 24
Last visit: Mon Feb 06, 2023 11:30 am
Has voted: 1 time

Re: Asynchronous Child forms

Post by gitpowershelluser »

may have been a bit tongue and cheek about reading..

To answer James first,
In my form the users get to choose a theme Light , Dark and once they make the selection, I write either Dark or Light to the registry so I can retrieve their choice when they restart the app

I have placed the function in my globals.PS1

In my childforms load event, which is now launches in a runspace. I use a switch statement to set the childforms theme on Load - if I remove this code no issues and as my image stated above, it’s this causing the error I posted.
  1. Switch($ThemeRegkey){
  2.  
  3. Dark
  4. {set-color theme -form $childform}
  5. Light
  6. {set-color theme -form $childform -Theme Light}
  7.  
  8. }
I’m going to be sticking around here so I might as well tell you my story before more assumptions are made

My org like others are in shortage of skilled workers. I know powershell and I sit on our L3 support and we need automation in GUI - I’m not a dev & ours are busy with business units that actually bring revenue in 😂 so I’m doing my best…. I do have more questions that I hope to address myself.

search for Sharing Variables and Live Objects Between PowerShell Runspaces learn-powershell
This topic is 2 years and 1 week old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked