Monotouch PerformSelector on specific thread with multiple arguments and callbacks

119 Views Asked by At

I've been having some issues with threading in monotouch. My app makes use of an external library which I've linked with and it works fine. Because of the nature of the app and the library I have to make all the calls to it on a single separate thread.These calls will generally be :

  • Random non deterministic caused by user
  • Every t miliseconds(around 20ms). Like an update function

After reading for a bit I decided to try out NSThread. I've managed to call the Update function by attaching an NSTimer to the thread's RunLoop and it's all working fine. The problem that I'm having now is calling other methods on the same thread. I read somewhere that using PerformSelector on the RunLoop adds the selector invocation to the RunLoop's queue and invokes it when available, which is basically exactly what I need. However the methods that I need to call :

  • Can have multiple paramteres
  • Have callbacks, which I need to invoke on the main thread, again with multiple parameters

For the multiple parameters problem I saw that NSInvocation can be a solution, but the life of me I can't figure out how to do it with monotouch and haven't found any relevant examples.

For the actuals calls that I need to make to the library, I tried doing a generic way in which I can call any function I choose via delegates on a particular thread, which sort of works until I'm hit with the multiple parameters and/or callbacks to the main thread again with multiple parameters. Should I maybe just register separate selectors for each (wrapped)function that I need to call from the library?

I'm not hellbent on using this approach, if there is a better way I'm open to it, it's just that after searching for other options I saw that they don't fit my case:

  • GCD(not even sure I have it in monotouch) spawns threads on it's own whenever necessary. I need a single specific thread to schedule my work on
  • NSInvocationQueue(which uses GCD internally from what I read) does the same thing.
  • pThreads, seem overkill and managing them will be a pain(not even sure I can use them in monotouch)

I'm not an iOS developer, the app works fine with monodroid where I had Runnables and Handlers which make life easier :) . Maybe I'm not looking at this the right way and there is a simple solution to this. Any input would be appreciated.

Thanks

UPDATE

I was thinking of doing something along these lines : Have a simple wrapper :

class SelectorHandler : NSObject
{
    public static Selector Selector = new Selector("apply");

    private Action execute;

    public SelectorHandler(Action ex)
    {
        this.execute = ex;
    }

    [Register("apply")]
    private void Execute()
    {
        execute();
    }
}

Extend NSThread

public class Daemon : NSThread
{
    public void Schedule(Action action)
    {
        SelectorHandler handler = new SelectorHandler(action);
        handler.PerformSelector(SelectorHandler.Selector, this, null, true);
    }
}

Then, when I want to call something I can do it like this :

private Daemon daemon;
public void Call_Library_With_Callback(float param, Action<int> callback)
{
    daemon.Schedule(() =>
    {
        int callbackResult = 0;
        //Native library calls
        //{
            // Assign callback result
        //}
        daemon.InvokeOnMainThread(() =>
        {
            callback(callbackResult);
        });
    });
}
0

There are 0 best solutions below