以下の内容はhttps://blog.okazuki.jp/entry/2016/04/08/163731より取得しました。


ReactivePropertyで2度押し防止

tamafuyou.hatenablog.com

qiita.com

CountNotifierを使うケースで。CountNotifierは、カウントを数えるものですが、Emptyの時だけというようにSelectかましてToReactiveCommandすると、多重実行防止にも使えます。こんな感じで。

public class MainWindowViewModel
{
    private CountNotifier ProcessCounter { get; } = new CountNotifier();

    public ReactiveCommand ExecuteCommand { get; }

    public ReactiveProperty<string> Output { get; }

    public MainWindowViewModel()
    {
        this.ExecuteCommand =
            this.ProcessCounter
                .Select(x => x == CountChangedStatus.Empty)
                .ToReactiveCommand();
        this.Output = this.ExecuteCommand
            .SelectMany(
                Observable.Using(
                    () => this.ProcessCounter.Increment(),
                    _ => this.HeavyTaskAsObservable()))
            .Select(x => x.ToString())
            .ToReactiveProperty();
    }

    // なんか重い処理
    public IObservable<DateTime> HeavyTaskAsObservable()
    {
        return Observable.Return(DateTime.Now).Delay(TimeSpan.FromSeconds(5));
    }
}

もうちょっと単純に書くとこういう風になるかな。

public class MainWindowViewModel
{
    private CountNotifier ProcessCounter { get; } = new CountNotifier();

    public ReactiveCommand ExecuteCommand { get; }

    public ReactiveProperty<string> Output { get; } = new ReactiveProperty<string>();

    public MainWindowViewModel()
    {
        this.ExecuteCommand =
            this.ProcessCounter
                .Select(x => x == CountChangedStatus.Empty)
                .ToReactiveCommand();

        this.ExecuteCommand.Subscribe(async _ =>
        {
            using (this.ProcessCounter.Increment())
            {
                this.Output.Value = (await this.HeavyTaskAsync()).ToString();
            }
        });
    }

    // なんか重い処理
    public async Task<DateTime> HeavyTaskAsync()
    {
        await Task.Delay(5000);
        return DateTime.Now;
    }
}

多重起動防止用のクラスがあってもいいかもしれないですね。




以上の内容はhttps://blog.okazuki.jp/entry/2016/04/08/163731より取得しました。
このページはhttp://font.textar.tv/のウェブフォントを使用してます

不具合報告/要望等はこちらへお願いします。
モバイルやる夫Viewer Ver0.14