Nulle
Nulle

Reputation: 1329

Sleep for x seconds before running next operation

I have been trying in various ways to make my program sleep for 10 seconds before running the next line of code.

this.SetContentView (Resource_Layout.Main)  
let timer = new System.Timers.Timer(10000.0)
async{do timer.Start()}
this.SetContentView (Resource_Layout.next)

I can't get any solution to work.

Upvotes: 2

Views: 84

Answers (1)

Tomas Petricek
Tomas Petricek

Reputation: 243106

If you want to use async rather than the more direct way (of creating a timer and setting the content view in the event handler of the timer), then you need something like this:

this.SetContentView (Resource_Layout.Main)  
async{
   do! Async.Sleep(10000.0)
   this.SetContentView (Resource_Layout.next) }
|> Async.StartImmediate

The key points:

  • Using do! Async.Sleep you can block the execution of asynchronous computation
  • By moving the SetContentView call inside the async, it will happen after the sleep
  • Using Async.StartImmediate, you start the workflow - and the sleeping ensures that the rest of the computation runs in the same threading context (meaning that it will run on the UI thread and the code will be able to access UI elements).

Upvotes: 4

Related Questions