# Avalonia.FuncUI - How to update ProgressBar value with MVU pattern?

**URL:** <https://forums.fsharp.org/t/avalonia-funcui-how-to-update-progressbar-value-with-mvu-pattern/2843>\
**Category:** General\
**Created:** [February 1, 2023, 1:33am UTC](https://forums.fsharp.org/t/avalonia-funcui-how-to-update-progressbar-value-with-mvu-pattern/2843 "2023-02-01T01:33:50Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![flx](https://yyz2.discourse-cdn.com/flex030/user_avatar/forums.fsharp.org/flx/32/1175_2.png) [@flx](https://forums.fsharp.org/u/flx)\
**Post date:** [February 1, 2023, 1:33am UTC](https://forums.fsharp.org/t/avalonia-funcui-how-to-update-progressbar-value-with-mvu-pattern/2843/1 "2023-02-01T01:33:50Z")

</div>

Hi,

I am new to Elm/Elmish/MVU. I am trying to to fetch some data after a button click. How can i update the progressbar while iterating the results?

Is there a way to create a dispatch event without any user interaction? My idea was to propagate the create a function that does the processing for the list head, and then sends the tail to Loading, but there seems to be no way to continue from there. The view is stuck as progressbar (value 1)

```auto
Elmish.Program.mkSimple (fun () -> MainView.init) MainView.update MainView.view
 ...
let update (msg: Msg) (state: State) : State =
        match msg with
        | LoadData ->
            match state with
            | Start s ->
                ...
                let deviceList = Api.Load()
                    deviceList
                    |> Array.iteri (fun i device->
                           (
                            // How to increment the progress bar here?
                            ...
                            )
                
                Loaded devices
            | Loading p -> Loading p
            | Loaded (data, getModel) -> Loaded (data, getModel)
 ...
 let rec view (state: State) (dispatch) =
    ...
    let rec view (state: State) (dispatch) =
        match state with
        | Start s ->
            StackPanel.create [
                StackPanel.spacing 8.0
                StackPanel.children [
                        TextBox.create [
                            TextBox.text "Start"
                        ]
                        Button.create [
                            Button.content "Load data from api"
                            Button.onClick (fun _ -> dispatch (LoadData))
                        ]
                ]
            ]
        | Loading progress ->
            StackPanel.create [
                StackPanel.spacing 8.0
                StackPanel.children [
                    ProgressBar.create [
                        ProgressBar.minimum 0.0
                        ProgressBar.maximum 100.0
                        ProgressBar.value progress.PercentageDone
                    ]
                ]
            ]
       ...

```

---

<div class="post-metadata">

**Author:** ![ntwilson](https://yyz2.discourse-cdn.com/flex030/user_avatar/forums.fsharp.org/ntwilson/32/984_2.png) [@ntwilson](https://forums.fsharp.org/u/ntwilson)\
**Post date:** [February 1, 2023, 11:17pm UTC](https://forums.fsharp.org/t/avalonia-funcui-how-to-update-progressbar-value-with-mvu-pattern/2843/2 "2023-02-01T23:17:49Z")

</div>

You might be interested in [this post](https://nozzlegear.com/blog/elmish-mksimple-vs-elmish-mkprogram), which talks about using `mkProgram` instead of `mkSimple` and introduces the concept of “Commands.” Some snippets that convince me it’s what you’re looking for:

> These commands are, among other things, a method for dispatching or even automating further changes to your model without any user input

> However, if you were to build this same program using Elmish’s `Program.mkSimple` you… **wouldn’t be able to do it at all** because `Program.mkSimple` only returns the model from its update function, not any commands.

Ultimately, you need your `update` function to be able to have some additional side-effects, instead of just returning a single updated `State`. And that’s what “Commands” are for.

---

<div class="post-metadata">

**Author:** ![flx](https://yyz2.discourse-cdn.com/flex030/user_avatar/forums.fsharp.org/flx/32/1175_2.png) [@flx](https://forums.fsharp.org/u/flx)\
**Post date:** [February 2, 2023, 11:41am UTC](https://forums.fsharp.org/t/avalonia-funcui-how-to-update-progressbar-value-with-mvu-pattern/2843/3 "2023-02-02T11:41:29Z")

</div>

The solution was using mkProgram and then passing a [subscription](https://elmish.github.io/elmish/docs/subscriptionv3.html) as the Cmd (Cmd.ofSub)

```auto
let apiLoadSubscription state: Sub<Msg> =
            let sub dispatch =
                 ...
                 Progress p |> dispatch
            sub

...

 let update (msg: Msg) (state: State) : State * Cmd<Msg> =
        match msg with
        | LoadData ->
            match state with
            | Start s ->
                state, Cmd.ofSub (apiLoadSubscription s)
...

```
