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):
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.
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
[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.
[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
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
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 httpclients are really slow with yield i think ?