Monotouch threads, GC, WCF

Posted by cvista on Stack Overflow See other posts from Stack Overflow or by cvista
Published on 2010-05-12T09:04:11Z Indexed on 2010/05/12 15:14 UTC
Read the original article Hit count: 306

Filed under:
|
|
|

Hi

This is a question about best practices i guess but it applies directly to my current MT project.

I'm using WCF services to communicate with the server.

To do this i do the following:

services.MethodToCall(params);

and the asynch:

services.OnMethodToCallCompleted += delegate{
    //do stuff and ting
};

This can lead to issues if you're not careful in that variables defined within the scope of the asynch callback can sometimes be cleaned up by the gc and this can cause crashes.

So - I am making it a practice to declare these outside of the scope of the callback unless I am 100% sure they are not needed.

Now - when doing stuff and ting implies changing the ui - i wrap it all in an InvokeOnMainThread call. I guess wrapping everything in this would slow the main thread down and rubbish the point of having multi threads.

Even though I'm being careful about all this i am still getting crashes and I have no idea why!

I am certain it has something to do with threads, scope and all that.

Now - the only thing I can think of outside of updating the UI that may need to happen inside of InvokeOnMainThread is that I have a singleton 'Database' class. This is based on the version 5 code from this thread http://www.yoda.arachsys.com/csharp/singleton.html

So now if the service method returns data that needs to be added/updated to the Database class -I also wrap this inside an InvokeOnMainThread call.

Still getting random crashes.

So... My question is this:

I am new to thick client dev - I'm coming from a web dev perspective where we don't need to worry about threads so much :)

Aside from what I have mentioned -are there any other things I should be aware of?

Is the above stuff correct? Or am i miss-understanding something?

Cheers

w://

© Stack Overflow or respective owner

Related posts about monotouch

Related posts about multithreading