Progress bar update during sort

Any way to update a progress bar during a long sort? I’ve fiddled with threads and timers and nada.

Using Windows 10.

“long sort” done in sql ? array ? listbox ?

not enough context to give any answer

Array sort. Takes about 8 seconds or so. I’ve fiddled with thread/timer approach but I think I’m missing something. During a sort does the thread still run?

No. You would have to sort it yourself, so you can yield time back to the UI.

Ah. So how do I show progress during a long sort? Is there any way? Even a spinning wheel would be nice but that freezes during the tight loops. Further, I can’t put the loop in a thread (I don’t think) because it adds rows to a listbox as part of its structure. I guess I could create a multi-dimensioned array for that and plug in the results after the thread is done running.

Just want to keep the user from thinking the machine is locking up. The whole loop, including sort, takes 20 seconds or so. It’s making a word frequency distribution of long texts, such as the Bible.

a) Separate the interface from the sort.
b) Adding rows is slow. Try to add them all at once.
c) Do you have something in your sort that can do a progress? The easiest way would be a loop for your words

for word as integer = 0 to CountOfWords 'do sort here next

Then word/CountOfWords is your progress.

Put the process in a thread. It’s the only way. Don’t use Array.Sort(). See the Task example for how to update the UI from a thread.

The sort method in array takes 8 seconds?
That’s unbelievable!

For my Check Writer program which contains Formatted Text Control, during printing of that control which is uninterruptible, I use a helper that displays the progressBar, and quit it through an IPCSocket command when printing is finished.

Create another program which sole purpose is to display the progress window, copy it to Resources, and when needed, launch it.

See http://documentation.xojo.com/index.php/IPCSocket as well as /examples/Communication/IPCSocket.xojo_project

To find the program in resources without difficulties, use Tim Parnell’s TPSF class at Modules For You! [Updated 12/17/2015] - Add-Ons - Xojo Programming Forum

Are you using Text vs. String? If so, that could account for it. Text handling is significantly slower on Windows than the Mac, and definitely slower than String in either case. (At least, for now.)

Perhaps post the code so we can see if there are optimizations possible?

You might also try putting your data into an in-memory SQLite database. That way you can sort in more than one way if necessary and adding and redoing the sort is trivial.

[quote=317995:@Christian Schmitz]The sort method in array takes 8 seconds?
That’s unbelievable![/quote]

It’s a sort of the entire Bible, 780,000 words. I didn’t time it exactly. I will … right now …

367 ticks. That includes spitting the text. To wit

WordFreq = split(Txt, " ") WordFreq.Sort

If there’s a faster sort, I’m open to it.

[quote=318035:@Kem Tekinay]Are you using Text vs. String? If so, that could account for it. Text handling is significantly slower on Windows than the Mac, and definitely slower than String in either case. (At least, for now.)

Perhaps post the code so we can see if there are optimizations possible?[/quote]

dim WordFreq() as string WordFreq = split(Txt, " ") ' txt is the entire Bible WordFreq.Sort

If you place this in a thread, you can display a progressBar.

Use a timer to access the progressBar from the thread.

See /Applications/Xojo 2016 Release 4.1/Example Projects/Desktop/Controls/ProgressBar.xojo_binary_project

[quote=317996:@Michel Bujardet]For my Check Writer program which contains Formatted Text Control, during printing of that control which is uninterruptible, I use a helper that displays the progressBar, and quit it through an IPCSocket command when printing is finished.

Create another program which sole purpose is to display the progress window, copy it to Resources, and when needed, launch it.

See http://documentation.xojo.com/index.php/IPCSocket as well as /examples/Communication/IPCSocket.xojo_project

To find the program in resources without difficulties, use Tim Parnell’s TPSF class at Modules For You! [Updated 12/17/2015] - Add-Ons - Xojo Programming Forum

Thx. Will check out.

I’ll give it a whirl. I’ve struggled with threads/timer in the past. I’m an “auto-didactic” programmer, a holder from the Commodore 64 days. Used to write articles and type-in programs back in the 80’s. It’s been a fairly big learning curve stepping up to OOP. I’m sure my code looks fairly ancient to you pros here but I somehow find a way to cobble together code.

tldr; I’m kinda slow.

Please time the Split vs. Sort separately and report back. Perhaps the bottleneck is in the former?

Don’t worry, Mac. Xojo makes it easy.

  • Drag a thread on a page
  • Add the Run event handler to it.
  • Take the code you posted and move it to the thread run event
  • At the place where was the code, simply enter :
Thread1.Run

That’s it for moving the code to the thread.

Since you know approximately the time it takes for the sort, no need for much complicated things :

  • Drag a progressbar to the page. Let us call it ProgressBar1
  • Drag a timer to the page
  • Make it Off
  • Make its period 20ms (approximately 1/60th a second)
  • For 8 seconds, make the maximum of the ProgressBar 480
  • Add the Action event handler to the timer
  • In that Action event, put

ProgressBar1.Value = Progressbar1.value + 1 If Thread1.State = 4 then ProgressBar1.Visible = False me.Mode = Timer.ModeOff end if

Finally, after Thread1.Run, add

Timer1.Mode = Timer.ModeMultiple

That’s it.

The split is 9 ticks. The sort was 377 this time. So the sort is the bottleneck.

Is it unreasonable for a sort to take 6 seconds? This is a big one.

BTW, I’m using Windows 10 platform, Pentium. I just put in a SSD which speeds up disk use but probably not a sort.

Hm. Doesn’t work. I put a break immediately after turning the timer on and it breaked (broke?) there … but only after the thread had completed its work. IOW, the thread was a tight loop that wouldn’t let go of itself.

I did follow your instructions to the letter. So I’m not sure what’s the issue.