Cysharp / R3

The new future of dotnet/reactive and UniRx.
MIT License
2.1k stars 92 forks source link

OnErrorResume did not work as expectation #192

Closed NeverMorewd closed 5 months ago

NeverMorewd commented 5 months ago
 public Observable<AutomationElement> ObserveElementsFromCurrentPoint(CancellationToken cancellationToken) 
 {
     return Observable.CreateFrom(t =>
     {
         return ElementsFromCurrentPoint(cancellationToken);
     });
 }

private async IAsyncEnumerable<AutomationElement> ElementsFromCurrentPoint([EnumeratorCancellation] CancellationToken cancellationToken)
{
    while (true)
    {
        if (cancellationToken.IsCancellationRequested)
        {
            break;
        }
        await Task.Yield();
        yield return ElementFromCurrentPoint();
    }
}

public AutomationElement ElementFromCurrentPoint()
{
    try
    {
        var element = _automationBase.FromPoint(Mouse.Position);
        return element;
    }
    catch (UnauthorizedAccessException)
    {
        // when this error is threw,the observable is interrupted!
        throw;
    }
}
NeverMorewd commented 5 months ago

My mistake.The UnauthorizedAccessException breaks the looping.