UI and Threads questions

I have a Canvas subclass that instantiate a Thread and wire the Run event to one method of this class using AddHandler()

First question: considering the Canvas is part of the UI, is that legal?
It works properly on RS2012r2.1 and Xojo did not complain with an exception, but I’d like to know what you think about.

In the same class, my thread code, which simply build some thumbnails in background, calls self.Invalidate to tell the canvas to refresh when possible.
This indeed fire an exception because I’m accessing the UI. It was expected, even if it worked fine in RS2012r2.1.

Here my question: is that really necessary to deny an Invalidate? I understand it access the UI, but really is not drawing or doing complicate things, just invalidate the canvas. But may be I’m underestimating the problem.

If, internally, an invalidate is just a “mark it for a repaint later” probably the framework can be tricked to make it thread safe. We probably need a word from @Joe Ranieri here.

Are you sure your problem is just the invalidate?

I would also like to get some clarity on calling Invalidate from threads. From the new Xojo user manuals, I understand that Control.Invalidate is preferred instead of Control.Refresh, since Invalidate lets the OS decide when it’s best to refresh a control?

My knowledge is very vague on the issue, but it would certainly be a huge benefit to have the ability to Invalidate my OpenGL surfaces from threads.

Sure, it’s the only problem.
For now I switched to InvalidateThreadSafeMBS and it works.
Moreover I find the MBS method useful because it permits me to invalidate just a rect and not the entire area.

You should definitely use Invalidate instead of Refresh. Just in few situations I had to still use Refresh to get the control refreshed in a particular moment, but for most cases Invalidate is better.

Why MBS? You can also invalidate a rect in RB. From the LR:

Oops! You are right. I was too confident on the AutoComplete and Status which only suggests the simple version.
However, since we have Refresh and RefreshRect, I first tried to do InvalidateRect, which clearly didn’t work. This expose a consistency problem on the framework. They definitely should adopt one of the two, but not both.

An invalidate touches the UI and isn’t thread-safe. Internally it calls NSView’s setNeedsDisplay: or setNeedsDisplayInRect: to implement this.

I believe the MBS ‘thread safe’ stuff just uses a timer-like system. Nothing you couldn’t implement yourself.

Then nothing Xojo couldn’t even more efficiently implement for me. :slight_smile:

About MBS I recall reading somewhere that uses Grand Central Dispatch on Mac.