When and why you should be using Threads

When and why you should be using Threads:

Quite often I see people making programming mistakes that will cost them dearly in the long run. They’ll do things like use App.DoEvents to keep UI updating properly, or they’ll make their program inaccessible by leaving tight loops run with no way to cancel them. These situations are perfect examples of when you should be using a thread.

Aaron Ballman has written a couple of posts about using threads in RealBasic, wish he’d written them a couple of days sooner as I’ve just recently made a couple of changes to CaseDetective using threads to help make the interface more responsive while grabbing data from the database.

My changes have turned out pretty good, working well, and a lot simpler in the end than I thought it might be. But I’ve still learnt a couple of things from Aarons post, in particular I’ll be changing my code to use a “MessageReceiver” to do the progress bar updates. That was a great idea Aaron, thanks!

He mentioned he might talk about resource management between threads and how to properly use the locking mechanisms REALbasic provides, which would be useful. I had to work out how to use semaphores myself because I was getting conflicts whereby the interface was in the process of updating from new data but another thread changed the data it was trying to use. Bang! OutOfBoundsExceptions all over the place! One App.DatabaseAccesssSemaphore signaled as soon as a thread started to run, and released when finished and all was back in control. And I didn’t really lose any responsiveness in the GUI.

Me likee threads!

Update (2005-09-02): Aaron has now written a nice article on Locking Mechanisms In REALBasic.

(Via Ramblings.)

No comments.

  1. Funny how all programming jive starts to look the same. I glossed over the first mention of “REALBasic” and thought you were talking about .NET until the third paragraph. Oooops, not OOP.

  2. Well, I guess most mature languages look similar in that they all need to provide the same basic functionality, so I don’t blame you for making that assumption at all.

    I know you’re a developer yourself flipdoubt, what language do you use then?

  3. Recently, I’ve been beating my head up against C#.

  4. Ah, you poor man!

    I tried VB.NET about 2 years ago, even bought it, but I found it very tricky, some things just seemed more difficult than they should be (particularly with regards to data handling).

    Then I tried RealBasic, and that was me, it was so straight forward, intuitive in comparison to .NET but very powerful. Maybe it’s just me, maybe some people feel the opposite, but RealBasic had everything I needed, with the added bonus of no huge .NET runtime, and the support for cross-platform development that I craved.

    OK, some things need improvements in RealBasic, but it’s gaining users very rapidly and along with that will come all the libraries you could need as well as the features Real Software are adding all the time.