C# 去边界异步属性刷新

C# 去边界异步属性刷新,c#,system.reactive,throttling,debouncing,C#,System.reactive,Throttling,Debouncing,如果您能帮助解决以下问题,我将不胜感激: 在我的类中有一个属性 string Foo {get;set;} 类中有一个刷新函数。其中有一个长时间运行的方法,用于更新 Foo = await Task.Run()... etc. 当每秒调用1000次刷新时,如何避免任务堆叠?去Bouncing?节流?怎么做?Rx在项目中可用,我使用的是dotnet core 2.2 类构造函数 阶级 如果您可以使用另一个软件包,我建议您使用它的ReactiveCommand,它将立即处理您的所有问题:

如果您能帮助解决以下问题,我将不胜感激: 在我的类中有一个属性

string Foo {get;set;}
类中有一个刷新函数。其中有一个长时间运行的方法,用于更新

Foo = await Task.Run()... etc. 
当每秒调用1000次刷新时,如何避免任务堆叠?去Bouncing?节流?怎么做?Rx在项目中可用,我使用的是dotnet core 2.2

类构造函数

阶级


如果您可以使用另一个软件包,我建议您使用它的ReactiveCommand,它将立即处理您的所有问题:

  var command = ReactiveCommand.CreateFromTask(async () =>
            { // define the work
                Console.WriteLine("Executing at " + DateTime.Now);
                await Task.Delay(1000);
                return "test";
            });

            command.Subscribe(res => {
                // do something with the result: assign to property
            });

            var d = Observable.Interval(TimeSpan.FromMilliseconds(500), RxApp.TaskpoolScheduler) // you can specify scheduler if you want
                .Do(_ => Console.WriteLine("Invoking at " + DateTime.Now))
                .Select(x => Unit.Default) // command argument type is Unit
                .InvokeCommand(command); // this checks command.CanExecute which is false while it is executing
产出:

Invoking at 2019-01-22 13:34:04
Executing at 2019-01-22 13:34:04
Invoking at 2019-01-22 13:34:05
Invoking at 2019-01-22 13:34:05
Executing at 2019-01-22 13:34:05
我知道该软件包主要用于UI开发,但很少有好的技巧,比如可以在任何地方使用的ReactiveCommand

注意等待命令。默认情况下,Execute不会检查命令是否可以执行


我认为这比您的解决方案更具可读性。

主要问题是每次调用RefreshFoo时都会创建新订阅。如果每秒调用RefreshFoo 1000次,您希望得到什么输出?
  var command = ReactiveCommand.CreateFromTask(async () =>
            { // define the work
                Console.WriteLine("Executing at " + DateTime.Now);
                await Task.Delay(1000);
                return "test";
            });

            command.Subscribe(res => {
                // do something with the result: assign to property
            });

            var d = Observable.Interval(TimeSpan.FromMilliseconds(500), RxApp.TaskpoolScheduler) // you can specify scheduler if you want
                .Do(_ => Console.WriteLine("Invoking at " + DateTime.Now))
                .Select(x => Unit.Default) // command argument type is Unit
                .InvokeCommand(command); // this checks command.CanExecute which is false while it is executing
Invoking at 2019-01-22 13:34:04
Executing at 2019-01-22 13:34:04
Invoking at 2019-01-22 13:34:05
Invoking at 2019-01-22 13:34:05
Executing at 2019-01-22 13:34:05