I have a class (which I have no control over) that has a long running synchronous function which returns T. I want to make this asynchronous by changing it to an IObservable that will nothing while the function is running and then OnNext(T) -> OnCompleted(). What is the simplest way to implement this?
class Foo
{
bool TakesAWhile();
}
class FooWrapper
{
Foo foo;
IObservable<bool> TakesAWhile()
{
// What goes here?
}
}
The following works, but I suspect that I’m missing something nicer.
public IObservable<bool> TakesAWhile()
{
var s = new Subject<bool>();
var worker = new BackgroundWorker();
worker.DoWork += (sender, e) =>
{
s.OnNext(foo.TakesAWhile());
s.OnCompleted();
};
worker.RunWorkerAsync();
return s.AsObservable<bool>();
}
It’s simple – just use
.Start().Try something like this:
In your code you’re looking for this: