我有以下类
namespace TickRateEntity
{
public struct TickRateData
{
public double tickrate;
}
public class TickRate
{
private TickRateData tickRateData;
//etc
}
}
如何更改这些类,使TickRate或TickRateData数据每n分钟发布一次双tickrate
?
假设你的tickRateData
以某种方式被填充,那么你可以做这样的事情:
public class TickRate : IDisposable
{
private TickRateData tickRateData;
private readonly IDisposable _subscription;
public IObservable<double> Stream { get; }
public TickRate(double intervalInMinutes)
{
var stream = Observable.Interval(TimeSpan.FromMinutes(intervalInMinutes))
.Select(_ => tickRateData.tickrate)
.Publish();
_subscription = stream.Connect();
Stream = stream;
}
public void Dispose()
{
_subscription?.Dispose();
}
}
假设您有一个签名为TickRate GetTickRate()
的方法,则它有效:
int n = 5;
IObservable<double> query =
from x in Observable.Interval(TimeSpan.FromMinutes(n))
let tickRate = GetTickRate()
select tickRate.tickRateData.tickrate;
IDisposable subscription = query.Subscribe(x => /* do something with `x` */);
否则,如果TickRate
是以其他方式生成的,我们需要知道如何回答这个问题。