« MBS REALbasic Plugins… | Home | MBS REALbasic Plugins… »

About new Call functions

In recent plugin prereleases we added functions like this:

CallMethodMBS(target as object, name as string, value... as variant) as boolean
CallMethodOnMainThreadMBS(target as object, name as string, value... as variant) as boolean
CallMethodOnThreadMBS(BackgroundThread as BackgroundThreadMBS, target as object, name as string, value... as variant) as boolean

They are very similar. Currently they can take up to three variants as parameters, but that number can be increased if you need. The number of parameters you pass must of course match the declaration of the method you want to call.

Now you may ask: What do this methods do and why should I use them?

Well, first they call a method for you. Something that you can do yourself. Let's say you have a method like this:

sub test(someParameter as variant)

Now you can call in your code this method like this: window1.test("Hello World"). But you can also use our CallMethodMBS function can call it this way: CallMethodMBS(window1, "test", "Hello World"). As you see you need to pass an object for the function to be searched on. As you pass the function name as a string, you can make it dynamically. From time to time a feature like this is requested on the mailing lists and forums, so here you have it. CallMethodMBS returns a boolean whether the call worked. It fails of course if the target object is nil or has no such method.

Next you can use CallMethodOnMainThreadMBS to call this method. While this function returns directly to you, the actual function call will happen later on the main thread. Normally within a few milliseconds, but that depends on how busy your application is. You need a function like this if you do some work in a background thread and you need to perform code on the main thread. There is code doing GUI functions which must be called on the main thread, because it will crash otherwise. Especially with the new Cocoa target we will see this need. That it works most cases on Carbon/Win32 code has its reason in the fact that the main thread is mostly idle at that time.

The third set of functions called CallMethodOnThreadMBS will create a new thread for you and call your method there. Is makes multi threading in REAL Studio much easier than before. So instead of calling window1.test(somedata), you can now call CallMethodOnThreadMBS(new BackgroundThreadMBS, window1, "Test", somedata). This will spin off a new thread and do the execution there, so your main thread has time for doing GUI actions. Your other code will run after this line just fine.

We need you to pass a new BackgroundThreadMBS object as the plugin can't create one on itself. Within some limitations you could build functions like this yourself, but our plugin gives you a convenience way to improve your times. Email us if you have more ideas for functions like this.
29 07 10 - 17:43
No comments

Remember personal info?

Emoticons / Textile

Hide email:

Small print: All html tags except <b> and <i> will be removed from your comment. You can make links by just typing the url or mail-address.