How to update a TextArea from a thread

I know you’re not allowed / permitted to update the GUI from a Thread.

I have a progress window with a Progressbar and a TextArea. I have a thread which is doing some intensive operations. I have a timer on the progress window which fires every second and updates the value of the Progressbar based on a variable in the thread. This makes sense to me.

There is also a string variable in the thread which contains what the thread is currently doing. What I don’t know how to do is append to the TextArea in the progress window what the thread is currently doing (i.e. a log). I know I could append the variable to the TextArea whenever the Timer fires but that won’t catch every action performed in the thread (as the timer fires every second but more than one action in the Thread occurs per second).

Does this make sense? What’s the best way to solve this?

Why not expose a variable that gets appended to by the thread then have your timer read that and put that in the textarea? That way the only appending is done in the thread and the timer only has to take the lot and replace what is in the textarea.

Or add to an array, then append that to the TextArea and clear the array. Use a type of semaphore to make sure the array is not being accessed by the thread while you’re reading from it. The AppendText method of a TextArea is very fast.

Use a timer, build a queue…

Or use SetTextThreadSafeMBS(text as string) method which we have also for Textarea in latest plugins.

Which version does it become prohibited in? I have rb12 r2 and can still do it.

Jym, it is only forbidden in Xojo due to new exception being raised in that case.
In older versions, this simply caused crashes.

I can still do it in Xojo, though I don’t have a licensed copy

Not in Cocoa.

Well that’s not very write once use across platforms then is it?

Great tips guys.

I wasn’t sure if simply appending text was the fastest or most sensible way to do it but I’m glad it is. Have just tested it in a tight loop of 4000 iterations and it worked well.