Process/load entries/app looks like freezing

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.
Locked
User avatar
n1mdas
Posts: 22
Joined: Thu Dec 28, 2017 1:05 pm

Process/load entries/app looks like freezing

Post by n1mdas »

Hello,

I am building a GUI App on 5.0 PS using JSON API: https://ip-api.com (Pro account) to retrieve IPv4s from an external txt file regardless of the filename.
The output, is then sent to XLSX file using https://github.com/dfinke/ImportExcel . When the PS GUI app reads a list below <=1KB txt file size the app operates correctly upon clicking the btn and closes after script execution but when supplying a larger file like >=300+ KBs upon clicking on the btn it 'sort of freezes' and doesn't show: "not responding"/crashing/hang it just does not respond by closing it from the 'X' what I have to do is terminate it via task manager. I tried using a for loop to process 60 IPs/entries every 60 seconds but it still operates in this way.

Could you provide best practices how to resolve/improve this?

jvierra
Posts: 14702
Joined: Tue May 22, 2007 9:57 am
Answers: 8
Has voted: 2 times
Been upvoted: 6 times

Re: Process/load entries/app looks like freezing

Post by jvierra »

hello n1mdas, The issue occurs because the request is not ending until the complete file is retrieved. This can take quite a while with large files. You can use a "job" to return the file and query for job completion with a timer tick.

User avatar
n1mdas
Posts: 22
Joined: Thu Dec 28, 2017 1:05 pm

Re: Process/load entries/app looks like freezing

Post by n1mdas »

Do you mean something like a stop watch?: https://mcpmag.com/articles/2017/10/19/ ... shell.aspx ? I have read several sources if this is used there can be memory issues. About the job option could you point me to some examples or should it be better done via job tracker? Should it be something like this?: https://www.sapien.com/blog/2011/08/09/ ... r-control/ or https://www.sapien.com/blog/2018/08/07/ ... functions/ . Thank you for the support/hints.

jvierra
Posts: 14702
Joined: Tue May 22, 2007 9:57 am
Answers: 8
Has voted: 2 times
Been upvoted: 6 times

Re: Process/load entries/app looks like freezing

Post by jvierra »

You are overcomplicating this. You asked why this is freezing. The cause is bad program design due to a lack of understanding of how Windows Forms work. Events freeze the form until the event ends. You either have to 2wait or use other methods to prevent the event from blocking. None of this has anything to do with memory or memory leaks. Properly designed PowerShell code used in a form cannot cause a memory leak so I suggest that you ignore things like that. Place the Json code in a job and execute it. Poll the job for completion or failure and retrieve the data, It only takes a few lines of code to do all of this.

The JobTracker custom control works well and the article will show you how to use it. If you design it correctly there should be no issues.

User avatar
n1mdas
Posts: 22
Joined: Thu Dec 28, 2017 1:05 pm

Re: Process/load entries/app looks like freezing

Post by n1mdas »

Thank you for the feedback.

Based on https://www.sapien.com/blog/2019/03/27/ ... plication/ while refactoring it to the app I had created previously I have some questions.

Could you advise what am I missing how to pipe the ForEach loop and call the Get-IPGeolocation function and remaining steps?

On the places where the '$richtextbox1.Lines += $results | select -ExpandProperty Fullname' is listed how to add a ForEach ($IP In $IPs) and then Get-IPGeolocation($IP) | Select-Object status, IP, City, Country, district, continentCode, mobile | Export-Excel -Titlebold -Append -AutoSize -TableStyle Dark3 -Path $OutputFile ?

The GUI app provides buttons:

1. To select any txt file then construct the Invoke-RestMethod -Method Get -Uri $request to pull the different objects predefined
2. While the script pulls the IPs ForEach IP to $outputfile

jvierra
Posts: 14702
Joined: Tue May 22, 2007 9:57 am
Answers: 8
Has voted: 2 times
Been upvoted: 6 times

Re: Process/load entries/app looks like freezing

Post by jvierra »

I cannot possibly rewrite your code for you . That is not the purpose of these forums.

Here are some good resources that will help you become proficient with PowerShell and Windows forms.

https://info.sapien.com/

https://www.sapien.com/books_training/W ... werShell-4

User avatar
n1mdas
Posts: 22
Joined: Thu Dec 28, 2017 1:05 pm

Re: Process/load entries/app looks like freezing

Post by n1mdas »

jvierra, thank you for the resources/feedback. I really appreciate it.
I am not asking anyone to rewrite any code etc. just looking for hints/links with further notes to read and get better or feedback based on the asked question that I struggle with at the moment and if on the right path. Unfortunately, I am not using Powershell and Windows Forms so often as I would like to.

User avatar
Alexander Riedel
Posts: 7607
Joined: Tue May 29, 2007 4:43 pm
Answers: 2
Been upvoted: 6 times

Re: Process/load entries/app looks like freezing

Post by Alexander Riedel »

Maybe this helps.
https://www.sapien.com/blog/2011/07/15/ ... ive-loops/

Adding elements to *any* control during handling an event will not update the control until the current event handler has finished.
You can call DoEvents to force processing accumulated events while you are still looping in another event handler.
Alexander Riedel
SAPIEN Technologies, Inc.

Locked