# Progress bar update during sort

**URL:** <https://forum.xojo.com/t/progress-bar-update-during-sort/36331>\
**Category:** Windows\
**Created:** [February 26, 2017, 12:55am UTC](https://forum.xojo.com/t/progress-bar-update-during-sort/36331 "2017-02-26T00:55:19Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mark\_Jordan](https://forum.xojo.com/letter_avatar_proxy/v4/letter/m/c6cbf5/32.png) [@Mark\_Jordan](https://forum.xojo.com/u/Mark_Jordan)\
**Post date:** [February 26, 2017, 12:55am UTC](https://forum.xojo.com/t/progress-bar-update-during-sort/36331/1 "2017-02-26T00:55:19Z")

</div>

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

Using Windows 10.

---

<div class="post-metadata">

**Author:** ![Norman\_P](https://forum.xojo.com/letter_avatar_proxy/v4/letter/n/858c86/32.png) [@Norman\_P](https://forum.xojo.com/u/Norman_P)\
**Post date:** [February 26, 2017, 1:04am UTC](https://forum.xojo.com/t/progress-bar-update-during-sort/36331/2 "2017-02-26T01:04:15Z")

</div>

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

not enough context to give any answer

---

<div class="post-metadata">

**Author:** ![Mark\_Jordan](https://forum.xojo.com/letter_avatar_proxy/v4/letter/m/c6cbf5/32.png) [@Mark\_Jordan](https://forum.xojo.com/u/Mark_Jordan)\
**Post date:** [February 26, 2017, 4:40am UTC](https://forum.xojo.com/t/progress-bar-update-during-sort/36331/3 "2017-02-26T04:40:08Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Tim\_Hare](https://forum.xojo.com/user_avatar/forum.xojo.com/tim_hare/32/176_2.png) [@Tim\_Hare](https://forum.xojo.com/u/Tim_Hare)\
**Post date:** [February 26, 2017, 4:45am UTC](https://forum.xojo.com/t/progress-bar-update-during-sort/36331/4 "2017-02-26T04:45:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Mark\_Jordan](https://forum.xojo.com/letter_avatar_proxy/v4/letter/m/c6cbf5/32.png) [@Mark\_Jordan](https://forum.xojo.com/u/Mark_Jordan)\
**Post date:** [February 26, 2017, 5:06am UTC](https://forum.xojo.com/t/progress-bar-update-during-sort/36331/5 "2017-02-26T05:06:01Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Beatrix\_Willius](https://forum.xojo.com/user_avatar/forum.xojo.com/beatrix_willius/32/282_2.png) [@Beatrix\_Willius](https://forum.xojo.com/u/Beatrix_Willius)\
**Post date:** [February 26, 2017, 6:01am UTC](https://forum.xojo.com/t/progress-bar-update-during-sort/36331/6 "2017-02-26T06:01:14Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Tim\_Hare](https://forum.xojo.com/user_avatar/forum.xojo.com/tim_hare/32/176_2.png) [@Tim\_Hare](https://forum.xojo.com/u/Tim_Hare)\
**Post date:** [February 26, 2017, 6:41am UTC](https://forum.xojo.com/t/progress-bar-update-during-sort/36331/7 "2017-02-26T06:41:23Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Christian\_Schmitz](https://forum.xojo.com/user_avatar/forum.xojo.com/christian_schmitz/32/158_2.png) [@Christian\_Schmitz](https://forum.xojo.com/u/Christian_Schmitz)\
**Post date:** [February 26, 2017, 8:53am UTC](https://forum.xojo.com/t/progress-bar-update-during-sort/36331/8 "2017-02-26T08:53:26Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Michel\_Bujardet](https://forum.xojo.com/user_avatar/forum.xojo.com/michel_bujardet/32/252_2.png) [@Michel\_Bujardet](https://forum.xojo.com/u/Michel_Bujardet)\
**Post date:** [February 26, 2017, 9:07am UTC](https://forum.xojo.com/t/progress-bar-update-during-sort/36331/9 "2017-02-26T09:07:51Z")

</div>

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](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](https://forum.xojo.com/15318-modules-for-you-updated-12-17/0)

---

<div class="post-metadata">

**Author:** ![Kem\_Tekinay](https://forum.xojo.com/user_avatar/forum.xojo.com/kem_tekinay/32/154_2.png) [@Kem\_Tekinay](https://forum.xojo.com/u/Kem_Tekinay)\
**Post date:** [February 26, 2017, 3:56pm UTC](https://forum.xojo.com/t/progress-bar-update-during-sort/36331/10 "2017-02-26T15:56:39Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Greg\_O\_Lone](https://forum.xojo.com/user_avatar/forum.xojo.com/greg_o_lone/32/49_2.png) [@Greg\_O\_Lone](https://forum.xojo.com/u/Greg_O_Lone)\
**Post date:** [February 26, 2017, 5:42pm UTC](https://forum.xojo.com/t/progress-bar-update-during-sort/36331/11 "2017-02-26T17:42:22Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Mark\_Jordan](https://forum.xojo.com/letter_avatar_proxy/v4/letter/m/c6cbf5/32.png) [@Mark\_Jordan](https://forum.xojo.com/u/Mark_Jordan)\
**Post date:** [February 26, 2017, 9:33pm UTC](https://forum.xojo.com/t/progress-bar-update-during-sort/36331/12 "2017-02-26T21:33:17Z")

</div>

[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.

---

<div class="post-metadata">

**Author:** ![Mark\_Jordan](https://forum.xojo.com/letter_avatar_proxy/v4/letter/m/c6cbf5/32.png) [@Mark\_Jordan](https://forum.xojo.com/u/Mark_Jordan)\
**Post date:** [February 26, 2017, 9:35pm UTC](https://forum.xojo.com/t/progress-bar-update-during-sort/36331/13 "2017-02-26T21:35:12Z")

</div>

[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
`

---

<div class="post-metadata">

**Author:** ![Michel\_Bujardet](https://forum.xojo.com/user_avatar/forum.xojo.com/michel_bujardet/32/252_2.png) [@Michel\_Bujardet](https://forum.xojo.com/u/Michel_Bujardet)\
**Post date:** [February 26, 2017, 9:35pm UTC](https://forum.xojo.com/t/progress-bar-update-during-sort/36331/14 "2017-02-26T21:35:29Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Mark\_Jordan](https://forum.xojo.com/letter_avatar_proxy/v4/letter/m/c6cbf5/32.png) [@Mark\_Jordan](https://forum.xojo.com/u/Mark_Jordan)\
**Post date:** [February 26, 2017, 9:36pm UTC](https://forum.xojo.com/t/progress-bar-update-during-sort/36331/15 "2017-02-26T21:36:11Z")

</div>

[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](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](https://forum.xojo.com/15318-modules-for-you-updated-12-17/0%5B/quote%5D)

Thx. Will check out.

---

<div class="post-metadata">

**Author:** ![Mark\_Jordan](https://forum.xojo.com/letter_avatar_proxy/v4/letter/m/c6cbf5/32.png) [@Mark\_Jordan](https://forum.xojo.com/u/Mark_Jordan)\
**Post date:** [February 26, 2017, 9:38pm UTC](https://forum.xojo.com/t/progress-bar-update-during-sort/36331/16 "2017-02-26T21:38:50Z")

</div>

> [@317980:@Tim Hare](#):
>
> 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.

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.

---

<div class="post-metadata">

**Author:** ![Kem\_Tekinay](https://forum.xojo.com/user_avatar/forum.xojo.com/kem_tekinay/32/154_2.png) [@Kem\_Tekinay](https://forum.xojo.com/u/Kem_Tekinay)\
**Post date:** [February 26, 2017, 10:05pm UTC](https://forum.xojo.com/t/progress-bar-update-during-sort/36331/17 "2017-02-26T22:05:30Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Michel\_Bujardet](https://forum.xojo.com/user_avatar/forum.xojo.com/michel_bujardet/32/252_2.png) [@Michel\_Bujardet](https://forum.xojo.com/u/Michel_Bujardet)\
**Post date:** [February 26, 2017, 10:08pm UTC](https://forum.xojo.com/t/progress-bar-update-during-sort/36331/18 "2017-02-26T22:08:03Z")

</div>

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 :

```auto
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

```auto
Timer1.Mode = Timer.ModeMultiple
```

That’s it.

---

<div class="post-metadata">

**Author:** ![Mark\_Jordan](https://forum.xojo.com/letter_avatar_proxy/v4/letter/m/c6cbf5/32.png) [@Mark\_Jordan](https://forum.xojo.com/u/Mark_Jordan)\
**Post date:** [February 27, 2017, 2:24am UTC](https://forum.xojo.com/t/progress-bar-update-during-sort/36331/19 "2017-02-27T02:24:08Z")

</div>

> [@318077:@Kem Tekinay](#):
>
> Please time the Split vs. Sort separately and report back. Perhaps the bottleneck is in the former?

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.

---

<div class="post-metadata">

**Author:** ![Mark\_Jordan](https://forum.xojo.com/letter_avatar_proxy/v4/letter/m/c6cbf5/32.png) [@Mark\_Jordan](https://forum.xojo.com/u/Mark_Jordan)\
**Post date:** [February 27, 2017, 2:37am UTC](https://forum.xojo.com/t/progress-bar-update-during-sort/36331/20 "2017-02-27T02:37:28Z")

</div>

> [@318078:@Michel Bujardet](#):
>
> Don’t worry, Mac. Xojo makes it easy … That’s it.

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.

[Next page](https://forum.xojo.com/t/progress-bar-update-during-sort/36331.md?page=2)
