wpf 轮询mysql数据库_c# – 使用Reactive Extensions进行数据库轮询

韩宜春
2023-12-01

这是使用Rx轮询另一个系统的相当经典的案例.大多数人会使用Observable.Interval作为他们的首选运算符,对大多数人来说,这样会很好.

但是,您对超时和重试有特定要求.在这种情况下,我认为你最好使用运算符的组合:

> Observable.Timer允许您在指定时间内执行查询

>超时识别和超出的数据库查询

> ToObservable()将任务结果映射到可观察序列.

>重试以允许您在超时后恢复

>重复以允许您在成功进行数据库查询后继续.这还将保持先前数据库查询的完成与下一个数据库查询的开始之间的初始时段/差距.

这个工作LINQPad片段应该显示查询正常工作:

void Main()

{

var pollingPeriod = TimeSpan.FromSeconds(5);

var dbQueryTimeout = TimeSpan.FromSeconds(10);

//You will want to have your Rx query timeout after the expected silence of the timer, and then further maximum silence.

var rxQueryTimeOut = pollingPeriod + dbQueryTimeout;

var scheduler = new EventLoopScheduler(ts => new Thread(ts) { Name = "DatabasePoller" });

var query = Observable.Timer(pollingPeriod, scheduler)

.SelectMany(_ => DatabaseQuery().ToObservable())

.Timeout(rxQueryTimeOut, Observable.Return("Timeout"), scheduler)

.Retry() //Loop on errors

.Repeat(); //Loop on success

query.StartWith("Seed")

.TimeInterval(scheduler) //Just to debug, print the timing gaps.

.Dump();

}

// Define other methods and classes here

private static int delay = 9;

private static int delayModifier = 1;

public async Task DatabaseQuery()

{

//Oscillate the delay between 3 and 12 seconds

delay += delayModifier;

var timespan = TimeSpan.FromSeconds(delay);

if (delay < 4 || delay > 11)

delayModifier *= -1;

timespan.Dump("delay");

await Task.Delay(timespan);

return "Value";

}

结果如下:

Seed 00:00:00.0125407

Timeout 00:00:15.0166379

Timeout 00:00:15.0124480

Timeout 00:00:15.0004520

Timeout 00:00:15.0013296

Timeout 00:00:15.0140864

Value 00:00:14.0251731

Value 00:00:13.0231958

Value 00:00:12.0162236

Value 00:00:11.0138606

样本的关键部分是….

var query = Observable.Timer(TimeSpan.FromSeconds(5), scheduler)

.SelectMany(_ => DatabaseQuery().ToObservable())

.Timeout(rxQueryTimeOut, Observable.Return("Timeout"), scheduler)

.Retry() //Loop on errors

.Repeat(); //Loop on success

 类似资料: