Advanced .Net UI

Tonight I will briefly describe a high level workflow for desktop UI. The syntactic sugar eases the difficulty of folding a sequence of asynchronous steps into a single process.


When are async sequences useful?

Basically when you want to go back to the UI dispatcher to interactively report the workflow progresses.

 let tasks =
     let initStatus  = (OutLookSucces, 0)
     |> AsyncSeq.ofSeq
     |> AsyncSeq.foldAsync (fun (s,i) p -> async {
         match s with
         | OutLookSucces -> 
             let text = email_msg + "\n working on " + p.cpty.Name + ": " + (i+1).ToString() + " of " + tot.ToString()
             do! Async.SwitchToContext ctx
             self.Status <- text
             self.TaskProgress <- 100. *(float (i+1)) / (float tot)
             p.is_marked <- false
             do! Async.SwitchToThreadPool()
             return mailer p i ctx
         | OutLookException exc -> return OutLookException exc, i
         })  initStatus                

The above code is advanced but still readable. In Haskell and Category Theory they speak about a catamorphism  over an F-algebra


How can I stop the workflow?

Again, it is autmagically managed by a cancellation token.

Cancellation Token


Ok, now I need some emphasis!

Start colouring the most important part of your text boxes.

 | false, true -> async {
         do! Async.SwitchToContext ctx  
         let run = new Run()
         run.Foreground <- Brushes.Red
         run.Text <- " no GBP invoices from Sap" 
         do! Async.SwitchToThreadPool()
         return OutLookSucces } |> Async.RunSynchronously

Find below a gist with the custom control for you

Of course you can safely mix C# and F#

One thought on “Advanced .Net UI

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s