0
我正在學習Rx.NET,並在下面的示例代碼中嘗試執行異步任務觀察日期時間選擇器值更改事件。 onNext運行完美,但onError和onComplete不。我究竟做錯了什麼?Rx.NET不會觸發onError也不onComplete
private void frmReporting_Load(object sender, EventArgs e){
dtPickerValueChanged = Observable.FromEventPattern(
ev => dtPickerFrom.ValueChanged += ev,
ev => dtPickerFrom.ValueChanged -= ev);
dtPickerValueChanged = dtPickerValueChanged.Merge(Observable.FromEventPattern(
ev => dtPickerTo.ValueChanged += ev,
ev => dtPickerTo.ValueChanged -= ev));
dtPickerValueChanged
.Where(x => dtPickerFrom.Value <= dtPickerTo.Value)
.Finally(() => { tsslStatus.Text = "Finally"; })
.ObserveOn(SynchronizationContext.Current)
.Subscribe(
onNext: async x => {
var result = await Test(dtPickerFrom.Value, dtPickerTo.Value);
MessageBox.Show(result);
},
onError: exception => { MessageBox.Show(exception.Message); },
onCompleted:() => { MessageBox.Show("Finished loading data"); });
}
public Task<string> Test(DateTime start, DateTime end){
return Task.Run(
async() =>
{
await Task.Delay(3000);
return start.ToString("dd/MM/yyyy - HH:mm:ss") + " | " + end.ToString("dd/MM/yyyy - HH:mm:ss");
});
}
你爲什麼期待'onError'或'onCompleted'射擊?沒有什麼是結束你的事件流或拋出錯誤。 – Jacob
如果我在onNext中添加一個拋出的新Exception(「whatever」),它將無法在onErrror上捕獲它。關於完成我怎樣才能結束可觀察到的它不是一次性的?我見過很多教程和代碼示例,但我從來沒有找到有關何時調用每個動作的好解釋。 –