I run UIThreadingWithTask.xojo_binary_project example project,and it’s simple task thread running, I see to the windows task monitor.
and the cpu of the exectution waste is higner than 20%.
the core code is:
Dim progressValue As Integer
While progressValue < 100
progressValue = progressValue + 1
// Do nothing for 1/4 second or so so folks can see the actual progress
Dim waitUntil As Integer = Ticks + 15
While Ticks < waitUntil
Wend
// Call UpdateUI with any parameters you need. This calls the UpdateUI event handler
// where you can directly access any UI controls on the Window.
// This specifies simple parameters using a Pair
// You can also pass values using a Dictionary
Me.UpdateUI("UIProgress":progressValue)
Wend
when task.Run() , cpu cost expensive. why?
I am very love xojo ,but concern about cpu and speed.as my application would needs thousands of threads,and hope each thread handlse somedata every minutes.
[quote=270758:@Tim Hare]The example is not a real-world example. In particular, you should never do
While Ticks < waitUntil
Wend
That’s what is burning the cpu.
[/quote]
Then that is a really bad code example. People use example code to learn, so what was whoever wrote that thinking???
Just like the IDE itself, documentation and examples may be subject to bugs and typos.
If you encounter corners of Xojo that could (or should) be enhanced, start Feedback, search if a similar entry exists and add points to it or file a new one. It may take some time, depending on the focus of the engineers, but you made sure it will be fixed and others wont have to stumble about such an irritation.
If anything, examples need community efforts. I recently sent a project to Paul Lefebvre so he can include it in examples. Likewise, sending in a corrected project seems to me like a very good idea.
Examples should be “best practise” code - otherwise what’s the point? Teaching bad habbits? Xojo can do it but I can’t be bothered to do it properly?
If something is worth doing, then it’s worth doing it right.
And while I agree that we should send in good example code for inclusion, it is Xojo’s task to make sure their example code is up to scratch, not ours.
That example is intended to show how to update the UI while a thread is intentionally using CPU. It’s an example of how Task works, not of how to write efficient threading code.
I would have rewritten it to have a function named “SomethingThatTakesALongTimeExampleMethod” which does what you’re doing to simulate a long process. This way a developer looking at it would know that this is something they need to replace. Ample comments would help too.
I find that many people really don’t understand the UIThreadingWithTask example. I use it (my own version of the TaskThread) extensively in a 90 minute video I created for use with TCPSockets. This is available to subscribers at http://xojo.bkeeney.com/XojoTraining. Comes with project file with source you can use in your own projects.
[quote=270850:@Bob Keeney]I would have rewritten it to have a function named “SomethingThatTakesALongTimeExampleMethod” which does what you’re doing to simulate a long process. This way a developer looking at it would know that this is something they need to replace. Ample comments would help too.
I find that many people really don’t understand the UIThreadingWithTask example. I use it (my own version of the TaskThread) extensively in a 90 minute video I created for use with TCPSockets. This is available to subscribers at http://xojo.bkeeney.com/XojoTraining. Comes with project file with source you can use in your own projects.[/quote]
good place but click “login button” there is no effect when login…
Don’t know what to tell you. Trust me when I say people scream bloody murder if the training app goes down. We do offer an offline version that doesn’t require and internet connection. http://www.bkeeney.com/allproducts/xojo-trainer/ Same videos and same project files just offline.
But that has to be explicitly said in the comments, like this
// The following code will simulate a processor-intensive task and stress the computer.
// It is not considered good code and should not be used in YOUR apps.
Otherwise users (and not just beginners either) will take it as gospel, as that is human nature: trust in code created by experts more than your own code.
But let us look at it the other way round: where are the example projects demonstrating best coding practise? Xojo example code used to be very buggy (in some parts Eddies Electronics wasn’t even working correctly), and Paul has done a great job fixing code bugs and improving the documentation. But I would hazard that part of Xojo’s reputation for creating buggy apps is due to coders not being shown how to do it properly. Articles about “defensive programming” are great, but if you never see it being used then your not going to use it either. How many times have you seen people being told to always check if a file is not NIL and exists?
Its a tricky balance to keep examples clear and concise vs showing how to best do something.
Examples are often stripped of “best practice” coding to make the example small, clear & understandable.
Usually they are to demonstrate a concept or a “how to” without all the error checks.
Adding in all the error checking etc may often serve to obscure the concept being shown.
But not having all that leads people to, as you say, assume this is “the best way” to do it.
And that too leads to problems as you note.