c# - IObservable<T>.ToTask<T> method returns Task awaiting activation -
why task
await forever?:
var task = observable .fromeventpattern<messageresponseeventargs>(communicator, "pushmessagerecieved") .where(i => i.eventargs.getrequestfromreceivedmessage().name == requestname) .select(i => i.eventargs) .runasync(system.threading.cancellationtoken.none) .totask(); task.wait();
i know "pushmessagerecieved"
fired; can set break point on select lambda , hit it. task.wait()
never moves.
better update: firstasync()
looking for:
public static task<messageresponseeventargs> handlepushmessagerecievedasync(this icommunicator communicator, requestname requestname) { if (communicator == null) return task.fromresult<messageresponseeventargs>(null); var observable = getcommunicatorobservableforpushmessagereceived(communicator); return observable .where(i => i.getrequestfromreceivedmessage().name == requestname) .select(i => i) .firstasync() .totask(); }
where getcommunicatorobservableforpushmessagereceived()
is:
static iobservable<messageresponseeventargs> getcommunicatorobservableforpushmessagereceived(icommunicator communicator) { if (communicatorobservableforpushmessagereceived == null) { communicatorobservableforpushmessagereceived = observable .fromeventpattern<messageresponseeventargs>(communicator, "pushmessagerecieved") .where(i => !ispreviousmessage(i.eventargs.getrequestfromreceivedmessage().eventid)) .select(i => i.eventargs); } return communicatorobservableforpushmessagereceived; }
update: horrific (but works):
public static task<messageresponseeventargs> handlepushmessagerecievedasync(this icommunicator communicator, requestname requestname) { if (communicator == null) return task.fromresult<messageresponseeventargs>(null); var completionsource = new taskcompletionsource<messageresponseeventargs>(); observable .fromeventpattern<messageresponseeventargs>(communicator, "pushmessagerecieved") .where(i => i.eventargs.getrequestfromreceivedmessage().name == requestname) .select(i => i.eventargs) .toevent().onnext += (args) => { if (args.response.errors != null && args.response.errors.any()) { completionsource.trysetexception(args.response.errors.select(j => new exception(j.errormessage))); } else { completionsource.trysetresult(args); } }; return completionsource.task; }
both runasync
, totask
yield the last value in observable. no value produced until observable completes. observables created fromeventpattern
not complete. need force them complete take
or until
.
i'll note runasync
, totask
redundant , there isn't need both.
in case, i'll assume interested in first value makes through filter:
var task = observable .fromeventpattern<messageresponseeventargs>(communicator, "pushmessagerecieved") .firstasync(i => i.eventargs.getrequestfromreceivedmessage().name == requestname) .select(i => i.eventargs) .totask(); task.wait();
Comments
Post a Comment