gpt4 book ai didi

c# - 如何异步执行 WhenAnyValue 委托(delegate)?

转载 作者:行者123 更新时间:2023-11-30 15:57:21 28 4
gpt4 key购买 nike

每次另一个属性发生变化时,我都会使用此代码生成一个“已计算”或“输出”属性。派生属性已正确生成,但由于 CreateBlurImage 运行时间有点长,因此 UI 在运行时会卡住。

以异步方式获得相同最终结果的正确方法是什么?

根据@Shane 的一些建议,编辑这是我的代码的当前版本,但在所有处理完成之前,UI 仍然保持卡住状态。请注意,如果重要的话,有相当多的级联更新:

    public ColetaIsis Model { get; private set; }

public string NomePaciente { get { return Model?.NomePaciente; } }
public DateTime DataRealização { get { return Model.DataRealização; } }



public BitmapSource Listras
{
get { return _listras; }
set { this.RaiseAndSetIfChanged(ref _listras, value); }
}
BitmapSource _listras;


public double[,] Grayscale { get { return _grayscale.Value; } }
readonly ObservableAsPropertyHelper<double[,]> _grayscale;

public double[,] BlurMenor { get { return _blurMenor.Value; } }
readonly ObservableAsPropertyHelper<double[,]> _blurMenor;

public double[,] BlurMaior { get { return _blurMaior.Value; } }
readonly ObservableAsPropertyHelper<double[,]> _blurMaior;

public double[,] Diferença { get { return _diferença.Value; } }
readonly ObservableAsPropertyHelper<double[,]> _diferença;


public BitmapSource FiltradaMenor { get { return _filtradaMenor?.Value; } }
readonly ObservableAsPropertyHelper<BitmapSource> _filtradaMenor;

public BitmapSource FiltradaMaior { get { return _filtradaMaior?.Value; } }
readonly ObservableAsPropertyHelper<BitmapSource> _filtradaMaior;

public BitmapSource ImagemDiferença { get { return _imagemDiferença?.Value; } }
readonly ObservableAsPropertyHelper<BitmapSource> _imagemDiferença;


public IEnumerable<ScatterPoint> Picos => _picos;
IEnumerable<ScatterPoint> _picos;








// CONSTRUTOR
public ColetaIsisViewModel(ColetaIsis model)
{
this.WhenAnyValue(x => x.Listras)
.Where(item => item != null)
.ObserveOn(RxApp.TaskpoolScheduler)
.Select(im => GetArray.FromChannels(im, 0, 1))
//.ObserveOn(RxApp.MainThreadScheduler)
.ToProperty(this, x => x.Grayscale, out _grayscale, scheduler:RxApp.MainThreadScheduler);

this.WhenAnyValue(x => x.Grayscale)
.Where(item => item != null)
.ObserveOn(RxApp.TaskpoolScheduler)
.Select(ar => Gaussian.GaussianConvolution(ar, 1.5))
//.ObserveOn(RxApp.MainThreadScheduler)
.ToProperty(this, x => x.BlurMenor, out _blurMenor, scheduler: RxApp.MainThreadScheduler);

this.WhenAnyValue(x => x.BlurMenor)
.Where(item => item != null)
.ObserveOn(RxApp.TaskpoolScheduler)
.Select(ar => Gaussian.VerticalGaussianConvolution(ar, 5))
//.ObserveOn(RxApp.MainThreadScheduler)
.ToProperty(this, x => x.BlurMaior, out _blurMaior, scheduler: RxApp.MainThreadScheduler);

this.WhenAnyValue(x => x.BlurMenor, x => x.BlurMaior)
.Where(tuple => tuple.Item1 != null && tuple.Item2 != null)
.ObserveOn(RxApp.TaskpoolScheduler)
.Select(tuple => ArrayOperations.Diferença(tuple.Item1, tuple.Item2))
//.ObserveOn(RxApp.MainThreadScheduler)
.ToProperty(this, x => x.Diferença, out _diferença, scheduler: RxApp.MainThreadScheduler);



this.WhenAnyValue(x => x.BlurMenor)
.Where(item => item != null)
.ObserveOn(RxApp.TaskpoolScheduler)
.Select(ar => { ConversorImagem.Converter(ar, out BitmapSource im); return im; })
//.ObserveOn(RxApp.MainThreadScheduler)
.ToProperty(this, x => x.FiltradaMenor, out _filtradaMenor, scheduler: RxApp.MainThreadScheduler);

this.WhenAnyValue(x => x.BlurMaior)
.Where(item => item != null)
.ObserveOn(RxApp.TaskpoolScheduler)
.Select(ar => { ConversorImagem.Converter(ar, out BitmapSource im); return im; })
//.ObserveOn(RxApp.MainThreadScheduler)
.ToProperty(this, x => x.FiltradaMaior, out _filtradaMaior, scheduler: RxApp.MainThreadScheduler);

this.WhenAnyValue(x => x.Diferença)
.Where(item => item != null)
.ObserveOn(RxApp.TaskpoolScheduler)
.Select(ar => { ConversorImagem.Converter(ar, out BitmapSource im); return im; })
//.ObserveOn(RxApp.MainThreadScheduler)
.ToProperty(this, x => x.ImagemDiferença, out _imagemDiferença, scheduler: RxApp.MainThreadScheduler);


Model = model;

Listras = Model.Listras; // fires up the initial cascading updates
}

最佳答案

这些方法中的任何一种都行得通吗?这样 CreateBlurImage 部分就完成了 UIThread

public MyClass()
{
this.WhenAnyValue(x => x.StripedImage)
.ObserveOn(RxApp.TaskpoolScheduler)
.Select(im => CreateBlurImage(im))
.ObserveOn(RxApp.MainThreadScheduler)
.ToProperty(this, x => x.Filtered, out _filtered);
}

或者我很确定在 ToProperty 上指定调度程序将与上面相同

public MyClass()
{
this.WhenAnyValue(x => x.StripedImage)
.ObserveOn(RxApp.TaskpoolScheduler)
.Select(im => CreateBlurImage(im))
.ToProperty(this, x => x.Filtered, out _filtered, scheduler:RxApp.MainThreadScheduler);
}

关于c# - 如何异步执行 WhenAnyValue 委托(delegate)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45108662/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com