How to return an IObservable<DialogResult> from ShowDialog

165 views Asked by At

I'm looking to create an extension method with the following signature:

public static IObservable<DialogResult> ShowDialog(this Form form);

I don't know how to get this to work. This is what I have so far:

Task<DialogResult> task = Task.Factory.StartNew(() =>
{
    return form.ShowDialog();
});
return task.ToObservable();

Edit: Cool, thanks guys. I really need to read up on this stuff some more!

1

There are 1 answers

2
Shlomo On

I imagine this would work, though I don't understand the point.

public static class Extensions
{
    public static IObservable<DialogResult> ShowDialogObservable(this Form form)
    {
        return Observable.Create<DialogResult>(o =>
        {
            o.OnNext(form.ShowDialog());
            return Disposable.Empty;
        });
    }
}