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
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 »

gitpowershelluser wrote: Thu Mar 17, 2022 10:54 pm 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.

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
As suspected, you are trying to call a function in one runspace from another runspace. This cannot be done. All code and functions must reside in the runspace that calls the function or runs the code. "globals.ps1" is only global to the runspace it exists in.

Why do you need a runspace for a child form. Forms in a runspace are NOT child forms. They are not called from any form. They are independent dialogs running on a thread and as such are isolated from other threads in the process.

If you are going to be responsible for building coded solutions then you should learn about processes, threads and execution environments and process and thread isolation. If you drop the idea of using runspaces this will make the learning curve less steep and shorter.

You can pass variables to and from child forms without using a runspace which is what you originally asked for. m Why you switched to thinking you needed a runspace is unknown.

To use a runspace you need to have a specific requirement that indicates that only a runspace will provide the desired solution. Nothing you have stated indicates that a runspace is needed.
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 »

One short note - I am not sure why you posted this line, "search for Sharing Variables and Live Objects Between PowerShell Runspaces learn-powershell"

Variables and live objects are not functions. Your design says you want to set the theme on the load event. You cannot access any code in the script that created the runspace. Just add the function to the event that calls the function or to the root where all of the events are defined. The root of the form code is global to the whole form.

One thing you seem to lack in PowerShell basics is a concept called scope. See the following.

About_Scopes
gitpowershelluser
Posts: 24
Last visit: Mon Feb 06, 2023 11:30 am
Has voted: 1 time

Re: Asynchronous Child forms

Post by gitpowershelluser »

Thank you for your time and understanding

"search for Sharing Variables and Live Objects Between PowerShell Runspaces learn-powershell" , I posted this because this page has a great rundown on runspaces.. I can't link yet because I'm so new

Scoping makes sense, runspaces make sense, reasons why NOT to use them makes sense & we do have a few use cases why we will use them

I just include the function into the Childform.. easy :)

Please don't misunderstand this as I'm all knowing - we all should learn everyday and I've done that and will continue, but I learn a bit differently than the rest. I know I can go to MS Docs for the C# breakdown but thats a bit over my head right now but I'm starting to piece it together.
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