开发者

Monotouch threads, GC, WCF

开发者 https://www.devze.com 2022-12-30 04:26 出处:网络
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.

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 o开发者_如何学编程f 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://


FYI, we got over this hump by keeping a reference to the original controller when the app finishes launching (in our case a navigation controller). The issue we were facing was that the GC was clearing up the controllers because it thought it was un-necessary because they were declared within the scope of the method.

I hope this answer helps someone else in the future.

Cheers.

0

精彩评论

暂无评论...
验证码 换一张
取 消