Need help with WPF and form stuck

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.
jvierra
Posts: 15221
Last visit: Wed Jun 29, 2022 5:42 pm
Answers: 23
Has voted: 4 times
Been upvoted: 21 times

Re: Need help with WPF and form stuck

Post by jvierra »

You asked for samples of the RichTextBox. That is what I have. If the code you posted is what you do with your eyes closed then you should start coding with your eyes open. I also recognize the code you posted as I wrote and posted that about 5 years ago although most of the changes have left only about 10 extremely obvious lines of the original. I suspect it was changed before you copied it from wherever you got it to begin with.

I am sorry that I cannot solve your issue. I tried your original code and I find that the form does not "stuck". Of course, I do not know what "form stuck" means. When I run the code nothing that I see can be called "stuck". What do you mean by "stuck"?

There are a huge number of technical problems that can cause weird behaviors for scripts in PowerShell that have nothing to do with the script but can be issues with the Windows system or caused by other subtle technical issues. Using GUIs in PowerShell can cause odd behaviors. Using WPF and runspaces can uncover odd behaviors due to external system issues.

The best I can do is to tell you that I see no issues when running your code as per your instructions. Changing the Boolean in "globals.ps1" made no difference other than displaying or not displaying the ProgressBar. The output lines appeared in both cases.
jvierra
Posts: 15221
Last visit: Wed Jun 29, 2022 5:42 pm
Answers: 23
Has voted: 4 times
Been upvoted: 21 times

Re: Need help with WPF and form stuck

Post by jvierra »

Here is another example that I wrote about seven years ago for someone who had a well asked question, so it was easy to provide a simple example.

The solution shows some things about how you can add events to a WPF form at any time and have them execute in the context of the form rather than in the calling context. This is one method of getting around limitations caused by single threading.

Somewhere I have an example that shows a way to run a "main form" under PowerShell without building a DLL. I haven't found that one yet.

This link has a number of comments that show different ways to approach elements of the solution.

Click to sort a WPF control
justdjbest
Posts: 7
Last visit: Tue May 24, 2022 7:18 am

Re: Need help with WPF and form stuck

Post by justdjbest »

jvierra wrote: Wed May 18, 2022 6:22 pm Here is another example that I wrote about seven years ago for someone who had a well asked question, so it was easy to provide a simple example.

The solution shows some things about how you can add events to a WPF form at any time and have them execute in the context of the form rather than in the calling context. This is one method of getting around limitations caused by single threading.

Somewhere I have an example that shows a way to run a "main form" under PowerShell without building a DLL. I haven't found that one yet.

This link has a number of comments that show different ways to approach elements of the solution.

Click to sort a WPF control
I just want to add information to the field while the script is running. in Windows.Forms this can be done via doevents(). The example in the first post shows exactly this problem.
jvierra
Posts: 15221
Last visit: Wed Jun 29, 2022 5:42 pm
Answers: 23
Has voted: 4 times
Been upvoted: 21 times

Re: Need help with WPF and form stuck

Post by jvierra »

justdjbest wrote: Fri May 20, 2022 7:30 am I just want to add information to the field while the script is running. in Windows.Forms this can be done via doevents(). The example in the first post shows exactly this problem.
Again, the examples show you how to do this. The code you posted also adds to the control with no issues, so I have no idea what you are asking about.
justdjbest
Posts: 7
Last visit: Tue May 24, 2022 7:18 am

Re: Need help with WPF and form stuck

Post by justdjbest »

jvierra wrote: Fri May 20, 2022 10:29 am
justdjbest wrote: Fri May 20, 2022 7:30 am I just want to add information to the field while the script is running. in Windows.Forms this can be done via doevents(). The example in the first post shows exactly this problem.
Again, the examples show you how to do this. The code you posted also adds to the control with no issues, so I have no idea what you are asking about.
ok. maybe someone else will answer me. I'm tired of explaining what the problem is. No, your example is not even close to the topic. Anyone knows about the form refresh problem while using WPF
jvierra
Posts: 15221
Last visit: Wed Jun 29, 2022 5:42 pm
Answers: 23
Has voted: 4 times
Been upvoted: 21 times

Re: Need help with WPF and form stuck

Post by jvierra »

As I posted, your original example works. If it doesn't work for you then the problem is with your computer system somewhere. We cannot help you with problems like that as you have produced no information that can be used even propose a guess. You need to try another computer system or come up with a description of the problem that can be repeated on anther computer.

Also, your original question is vague. Do you want runspaces or not? If you don't want runspaces then you will not be able to run any scripts while the WPF form is displayed. This is one of the issues with running any forms in PowerShell due to its single threaded nature. Runspaces decouple the WPF form or any form or code from the main PowerShell script. This keeps both threads alive until either is blocked by something in the script that form is processing. There is no "DoEvents" for WPF. WPF would use a thread to decouple a long running script and use an event to detect that the code has completed. This is what I mean about coded systems and code design and the underlying technology.

Your question or problem as stated makes it impossible to know what your issue is. You need to think it through and ask a better and more specific question. Remember, there is no "Do Events" in WPF due to WPF requiring two threads to work as designed. A WPF dialog is just like a WinForms dialog in that is single threaded. Block anywhere in any part of the thread blocks the thread completely. WinForms has "DoEvents" but WPF is designed differently and we wouldn't use a Dialog for any lengthy process in a WPF dialog except if a "frozen" dialog was acceptable.
Locked