Thread with UI Update

Hey there,

actually i testing around with threads… but i really dont understand this threading in xojo.

i tried a little test for me, i have a listbox and for every entry i want to start a thread task.

for i as integer = 0 to main.listbox1.listcount -1 Dim refreshThread as new TestThread(i,main.listbox1.Cell(i,0),main.listbox1.Cell(i,1),main.listbox1.Cell(i,3)) refreshThread.Run next

i would now expect that for each entry will be a thread started here. Then i tried to get a timer for this UI actions…

The thread will do this at run:

main.UpdateTimer.Mode = Timer.ModeSingle

The Timer will do this (write a entry to a logfile):

PreferencesModule.Log("Thread","Tick") me.Mode = Timer.ModeOff

So i only get one Tick off for example 6 entries…? Is there a simple project how a thread will work properly with the UI?

Yep in New Project / Examples / Desktop / Threading / Threading Example you’ll find a good example. In Addition: Brad Rhyne shows in his ebook “Introduction to programming with Xojo” an ideal example how to create snappy UI with treads. You’ll find this eBook on Xojos’ homepage.

Ok thank you :slight_smile: i will check the book. The Xojo example seems complicated for me…

We have our own version, BKS Thread that’s based on the Task Thread example, that’s available as a video at http://xojo.bkeeney.com/XojoTraining for subscribers. In addition you can get over 65 hours of other training video as well as over 200 videos (most with project files with source you can use in your own app). For some people a video showing it in use is easier to understand. For others, not so much. But it is an option.

At the end of each for loop pass the thread created at the start of the for loop pass is destroyed. You need to keep a reference to each thread instance outside of the loop. Something like this:

Property refreshThreads() As Thread // a property of the windows ... For i As Integer = 0 To main.Listbox1.Listcount -1 refreshThreads .Append New TestThread(i,main.Listbox1.Cell(i, 0), main.Listbox1.Cell(i, 1), main.Listbox1.Cell(i, 3)) refreshThread.Run() Next

hm i get it now, but i really wonder how so many threads can bring me different random numbers but with all the same thread id… makes no sense to me

Anyway, so many threads will only slow down the app. And if you have as many timers, they will fight for a moment to fire.

You may want to consider a single thread, a single timer, and a queue of operations.

[quote=305830:@Sascha Mierke]hm i get it now, but i really wonder how so many threads can bring me different random numbers but with all the same thread id… makes no sense to me

[/quote]
If your thread doesn’t yield, they’re running one at a time.

What code is being run by the thread?

[quote=305838:@Michel Bujardet]Anyway, so many threads will only slow down the app. And if you have as many timers, they will fight for a moment to fire.

You may want to consider a single thread, a single timer, and a queue of operations.[/quote]

I dont think this is actual a problem, i have one timer which handle the threads. And the threads will be created like this:

For i as Integer = 0 to Listbox1.ListCount-1 Dim t as new uThread t.run wUThread().Append(t) Next

[quote=305841:@Greg O’Lone]If your thread doesn’t yield, they’re running one at a time.

What code is being run by the thread?[/quote]

Just this simple test:

Dim r As New Random Dim i As Integer = r.InRange(0, 1000) save = "Random Number: " + i.ToText id = me.ThreadID

You are missing the point.

Each time you create a thread, it takes execution time away from the main thread. Meaning, it slows it down. That is a fact.

If on top of it you never yield, that is a very bad construct. But hey, that is your karma…

I may add that Windows can have only so many threads, based on resource constraints.

The thread code is like the one from the example WordCountGui, so i guess the xojo people will handle the threading right? Or is something wrong with this?

I know that a thread costs execution time, but there wll be not hundred of threads, like a chrome browser or something :slight_smile:

It is all a question of rational usage or resources. Threads by themselves are not bad. Misuse of threads is.

The example is just that. An example of what can be done. It does not means you have to do it that way.

Actual i tried it in my app which downloads some informations from listbox entries, without threads it took over 10-15 seconds to collect all informations…now it tooks mabye 2 :slight_smile: httpclients are really slow with yield i think ?

HTTPSocket in asynchronous mode does not need any thread, and it relinquishes UI immediately.