PerformSelector extension

By July 24, 2012Blog

While back I wrote an post about perform selector helper with pure delegates and a helper class. I then changed it a bit to use NSAction for easier use. So here it is:

 

namespace MonoTouch.Foundation.Extensions
{
	public static class CoreFoundationExtensions
	{
		public static void PerformSelector (this NSObject obj, NSAction action, float delay)
		{
			int d = (int)(1000 * delay);
 
			var thread = new Thread(new ThreadStart ( () => {
				using(var pool = new NSAutoreleasePool())
    				{
						Thread.Sleep (d); 
						action.Invoke ();
    				}
			 }));			
 
			thread.IsBackground = true;
			thread.Start();
		}
 
		public static void PerformSelector (this NSObject obj, NSAction action)
		{
			PerformSelector (obj, action, 0.001f);
		}
	}
}

 

Happy coding!