Hi all. My question is: how does it work? Attached code. Run Globals.ps1 via Sapien Powershell Studio. change true/false in the first line
When the Write-Progress is displayed, the form does not stuck, but without it - stuck. Why?
I understand Write-Progress is just "on/off" its form and during this time the main WPF form is updated?
I have a lot of code under System.Windows.Forms and I wanted to switch to WPF with minimal code rewriting, so I'm looking for any solutions. I know that it can be solved through runspaces, but this is not very suitable for me (it will be necessary to rewrite the logic of 50+ modules, which can take weeks of my life). I ask everyone who has ideas to respond.
Need help with WPF and form stuck
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.
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.
-
- Posts: 8
- Last visit: Sat Sep 17, 2022 11:22 am
Re: Need help with WPF and form stuck
WPF main forms will not work correctly in PowerShell. WPF forms require a multi-threaded environment. PowerShell is single threaded only.
PowerShell studio allows us to create WPF controls. Some controls will work but not all WPF designed controls will work easily with PowerShell even with the Sapien custom control set.
PowerShell studio allows us to create WPF controls. Some controls will work but not all WPF designed controls will work easily with PowerShell even with the Sapien custom control set.
-
- Posts: 8
- Last visit: Sat Sep 17, 2022 11:22 am
Re: Need help with WPF and form stuck
But there are workarounds through runspaces+hashtables and its works fine. I just don't want to rewrite a lot of code, but this is not a problem for the PS. There are simpler, but less informative options, like jobs.jvierra wrote: ↑Tue May 17, 2022 10:06 am WPF main forms will not work correctly in PowerShell. WPF forms require a multi-threaded environment. PowerShell is single threaded only.
PowerShell studio allows us to create WPF controls. Some controls will work but not all WPF designed controls will work easily with PowerShell even with the Sapien custom control set.
Re: Need help with WPF and form stuck
Even with runspaces you cannot create a multi-threaded runspace. A runspace is a single thread and so is a job.
Your original question is very vague. It sounds like you are in a loop and the progress bar is using a timer. A timer event can allow a blocked event to act like it is not blocked whenever the timer ticks.
I will take a quick look at your code. I just wanted you to understand that a WPF form can cause all kinds of strange behaviors when used with PowerShell. When you create a runspace in PowerShell you are still running a PowerShell script under a PowerShell engine. Runspaces do not eliminate the issues of trying to run a multi-threaded from in a single-threaded process.
Your original question is very vague. It sounds like you are in a loop and the progress bar is using a timer. A timer event can allow a blocked event to act like it is not blocked whenever the timer ticks.
I will take a quick look at your code. I just wanted you to understand that a WPF form can cause all kinds of strange behaviors when used with PowerShell. When you create a runspace in PowerShell you are still running a PowerShell script under a PowerShell engine. Runspaces do not eliminate the issues of trying to run a multi-threaded from in a single-threaded process.
Re: Need help with WPF and form stuck
I don't see what doesn't work. It works fine for me, and it doesn't matter if the progress bar is used or not. Without the pbar it takes about twice as long to execute which is around 200ms with and 1 second without.
I see no reason to run code in "globals". Did you just borrow or copy the files from a project and modify them? Likely it is your execution environment setup that is causing you problems.
Rule #1: avoid global variables except when absolutely necessary. Always define variables within the scope used.
You are not using jobs; you are just creating an array called "$jobs". Avoid using keywords in any language to define variables.
I would start by putting everything in one file and defining everything at the same scope level. This will eliminate your environmental issues. Running this in PSS can also create issues when launching runspaces. Test at a real PowerShell prompt. This will end up becoming your debugging fallback when working with WPF.
When I build WPF forms I create them in Visual Studio in C# then put them in a DLL and call the form from script. This allows us to use the "Run" method which creates the corrected threaded environment for WPF forms. It also isolates the form from PowerShell bu you lose the ability to interact with the form. The PS "ShowDialog" for a WPF form allows the form to exist on the PS thread or in a runspace but communication is still limited.
PS is not designed to be an applications language. It is an automation language and neither WPF nor WinForms were part of the original PS design so they both of severe limitations and often strange quirks.
I see no reason to run code in "globals". Did you just borrow or copy the files from a project and modify them? Likely it is your execution environment setup that is causing you problems.
Rule #1: avoid global variables except when absolutely necessary. Always define variables within the scope used.
You are not using jobs; you are just creating an array called "$jobs". Avoid using keywords in any language to define variables.
I would start by putting everything in one file and defining everything at the same scope level. This will eliminate your environmental issues. Running this in PSS can also create issues when launching runspaces. Test at a real PowerShell prompt. This will end up becoming your debugging fallback when working with WPF.
When I build WPF forms I create them in Visual Studio in C# then put them in a DLL and call the form from script. This allows us to use the "Run" method which creates the corrected threaded environment for WPF forms. It also isolates the form from PowerShell bu you lose the ability to interact with the form. The PS "ShowDialog" for a WPF form allows the form to exist on the PS thread or in a runspace but communication is still limited.
PS is not designed to be an applications language. It is an automation language and neither WPF nor WinForms were part of the original PS design so they both of severe limitations and often strange quirks.
-
- Posts: 8
- Last visit: Sat Sep 17, 2022 11:22 am
Re: Need help with WPF and form stuck
do you have a couple of examples?jvierra wrote: ↑Tue May 17, 2022 3:46 pm When I build WPF forms I create them in Visual Studio in C# then put them in a DLL and call the form from script. This allows us to use the "Run" method which creates the corrected threaded environment for WPF forms. It also isolates the form from PowerShell bu you lose the ability to interact with the form. The PS "ShowDialog" for a WPF form allows the form to exist on the PS thread or in a runspace but communication is still limited.
-
- Posts: 8
- Last visit: Sat Sep 17, 2022 11:22 am
Re: Need help with WPF and form stuck
I just need one thing - add text in richtextbox when something is in progress if use wpfjvierra wrote: ↑Tue May 17, 2022 3:16 pm Even with runspaces you cannot create a multi-threaded runspace. A runspace is a single thread and so is a job.
Your original question is very vague. It sounds like you are in a loop and the progress bar is using a timer. A timer event can allow a blocked event to act like it is not blocked whenever the timer ticks.
Another way - using stock forms, adding and using third party dll like this github.com\leocb\MaterialSkin BUT i have no idea how to do it. I will be glad to hear your ideas. if it works out, many will be interested in transforming ugly forms into beautiful ones
Re: Need help with WPF and form stuck
If you want the new sexy WPF forms then you will have to create them in Visual Studio as doing it with PowerShell is much harder, takes longer, requires a deeper technical knowledge of Windows, Net Framework and PowerShell. PS can only easily make simple WPF dialogs and managing them is hard.
I can find an example of how to use and RTB in a WPF dialog from PowerShell.
There are numerous books available on WPF. For me to teach you would be nearly impossible due to time constraints and the fact that Microsoft and others have professional courses for WPF developers.
I can find an example of how to use and RTB in a WPF dialog from PowerShell.
There are numerous books available on WPF. For me to teach you would be nearly impossible due to time constraints and the fact that Microsoft and others have professional courses for WPF developers.
Re: Need help with WPF and form stuck
Here is a sampler of a number of things you can do with WPF including an RTB.
- Attachments
-
- Test-XamlDialog.ps1
- (5.43 KiB) Downloaded 81 times
-
- Posts: 8
- Last visit: Sat Sep 17, 2022 11:22 am
Re: Need help with WPF and form stuck
but it is not what I need... Such things I already do with my eyes closed.