Is there an easy way to execute a method after a given delay, for example in iOS?
On an iPhone, I would do this:
[self performSelector:@selector(connectSensor) withObject:nil afterDelay:2.5];
Then he will pay the connectSensor method in the main thread (UI thread), which will be executed in 2.5 seconds. And since it is automatically assigned in the main thread, you do not need to worry about cross-threading issues. (There is also a version of performSelectorOnBackground )
So, how would I do it right in WP7?
I am currently doing this with a timer, but I'm not sure if this is a good solution.
private Timer timer; private void DoSomethingAfterDaly() { // ... do something here timer = new Timer( (o) => Deployment.Current.Dispatcher.BeginInvoke(() => NavigationService.GoBack()), null, 2500, Timeout.Infinite); }
How can this be encapsulated in an extension method, so I can just call this.Perform(MyMethod, null, 2500); ?
Buju
source share