Hi,

you should use System.Threading.ThreadPool instead:
http://docs.xamarin.com/ios/advanced_topics/threading
The main advantage: the threads are managed in a pool (who would have
guessed?) and you don't need to worry about creating an auto release pool.
Apart from that, your current code looks okay but you don't need all those
Export() attributes, do you?
If you want to end a long running thread, you can either Abort() it or
Interrupt() it (read about the difference on MSDN). If you abort you might
create leaks or other unwanted situations as your code execution stops right
where it is.
Starting with .Net 3 I think Thread.Abort() no longer interrupts finally
blocks, so Abort() is not that evil anymore. I am not sure if a constructor
is considered a safe block. Microsoft is unclear about it and I don't know
how it is handled in MONO and if something like "var a = new A();" can fail
at the "=" sign.
The safer way is to set a bool (if you only have one thread writing to it)
or use an AutoResetEvent and set it if you want to abort. Your thread then
has to check regularly if the flag is set and has to exit ASAP.

If your target is to check for internet connectivity, you could also have a
look at Miguel's Reachability class:
https://github.com/xamarin/monotouch-samples/blob/master/ReachabilitySample/
reachability.cs
With that you can even get notified if  connectivity changes.

René

-----Ursprüngliche Nachricht-----
Von: [email protected]
[mailto:[email protected]] Im Auftrag von simarx
Gesendet: Freitag, 4. November 2011 15:56
An: [email protected]
Betreff: [MonoTouch] 2 Threading Questions ...

Hi - I have the following code that kicks of a new thread (the thread is
designed to check for internet reachability) and at the end check I want it
to call a method on the main UI thread. Two questions really...

1. Is the code safe from memory-leaks, in particular the way my "EndCheck"
method is called from within the thread?

2. If during the process I wish to terminate the busy new thread, how can I
safely do that?

-----------

public void InternetCheckShowBusyView(string sMessage) {
        Thread thread = new System.Threading.Thread(DoCheckInternet as
ThreadStart);
        thread.Start();
}               

[Export("DoCheckInternet")]
void DoCheckInternet()
{
        using (var pool = new NSAutoreleasePool())
        {
                bool bResult = MyInternetCheckMethod();
                InvokeOnMainThread(EndCheck);
        }
}
                
[Export("EndCheck")]
void EndCheck()
{
        // Do whatever I want...
}


--
View this message in context:
http://monotouch.2284126.n4.nabble.com/2-Threading-Questions-tp3990347p39903
47.html
Sent from the MonoTouch mailing list archive at Nabble.com.
_______________________________________________
MonoTouch mailing list
[email protected]
http://lists.ximian.com/mailman/listinfo/monotouch

_______________________________________________
MonoTouch mailing list
[email protected]
http://lists.ximian.com/mailman/listinfo/monotouch

Reply via email to