Coder Social home page Coder Social logo

Comments (6)

FoggyFinder avatar FoggyFinder commented on September 26, 2024

A similar question has already been asked on SO:

http://stackoverflow.com/questions/35554054/f-wpf-async-command-completion-status/35558857

Thus:

    [<STAThread; EntryPoint>]
    let main argv = 
        if SynchronizationContext.Current = null then
            DispatcherSynchronizationContext(Dispatcher.CurrentDispatcher)
            |> SynchronizationContext.SetSynchronizationContext
        let win = MainWindow()
        win.Root.DataContext <- MainWindowViewModel()
        let app = Application()
        app.Run(win.Root)

from fsharp.viewmodule.

WallaceKelly avatar WallaceKelly commented on September 26, 2024

@FoggyFinder Thank you for the SO link. I had not found that.

I added the code to SetSynchronizationContext to my demo. It is an improvement. The button becomes re-enabled when the asyncWorkflow completes. However, I want to do some more research. The behavior is still not exactly what I would have expected. (I would not have expected the button to be disabled while the asyncWorkflow runs.) But, my expectations may be the problem. Thanks again!

from fsharp.viewmodule.

ReedCopsey avatar ReedCopsey commented on September 26, 2024

@WallaceKelly we don't always disable, because there are times when people want to allow more than one execution.

If you make can execute return not self.OperationExecuting it'll disable and re-enable automatically

from fsharp.viewmodule.

WallaceKelly avatar WallaceKelly commented on September 26, 2024

@ReedCopsey I'm glad to hear how you describe it, because that is what I expected. However, that is not what I'm seeing. The behavior I'm seeing is that the button becomes disabled, only allowing one execution, even if my canExecute always returns true.

from fsharp.viewmodule.

ReedCopsey avatar ReedCopsey commented on September 26, 2024

@WallaceKelly I'm not sure I follow - you definitely need @FoggyFinder 's workaround (the Sync Context setup I posted in SO). That's actually an issue in C# if you build your own app, too, and use async/await heavily in the VM.

Once that's there, if you change your CanExecute as I mentioned, it should enable/disable:

let canExecute _ = not self.OperationExecuting
member val DoSomethingAsync =
        this.Factory.CommandAsyncChecked(asyncWorkflow, canExecute, [ <@ this.OperationExecuting @> ])

This sample from FsXaml shows some ideas around using operation executing: https://github.com/fsprojects/FsXaml/blob/master/demos/WpfMvvmAgent/MainViewModel.fs

If it's still not working, can you post a full repro? I'll take a look.

from fsharp.viewmodule.

FoggyFinder avatar FoggyFinder commented on September 26, 2024

(I would not have expected the button to be disabled while the asyncWorkflow runs.)

@WallaceKelly, You mean something like this?

    let asyncWorkflowS() =
        async {
            do! Async.Sleep(3000)
            System.Diagnostics.Debug.WriteLine("asyncWorkflow completed.")
        } |> Async.Start

    member val DoSomethingAsync =
        this.Factory.CommandSyncChecked(asyncWorkflowS, canExecute)

from fsharp.viewmodule.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.