本文整理汇总了C#中AsyncSubject.OnError方法的典型用法代码示例。如果您正苦于以下问题:C# AsyncSubject.OnError方法的具体用法?C# AsyncSubject.OnError怎么用?C# AsyncSubject.OnError使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类AsyncSubject
的用法示例。
在下文中一共展示了AsyncSubject.OnError方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: WriteAsync
public static IObservable<Unit> WriteAsync(this Stream This, byte[] data, int start, int length)
{
var ret = new AsyncSubject<Unit>();
try
{
This.BeginWrite(data, start, length, result =>
{
try
{
This.EndWrite(result);
ret.OnNext(Unit.Default);
ret.OnCompleted();
}
catch (Exception ex)
{
ret.OnError(ex);
}
}, null);
}
catch (Exception ex)
{
ret.OnError(ex);
}
return ret;
}
示例2: detect
private IObservable<string> detect(string text)
{
var subject = new AsyncSubject<string>();
string detectUri = String.Format(GetDetectUri(text), appId, HttpUtility.HtmlEncode(text));
var wc = new WebClient();
wc.OpenReadCompleted += new OpenReadCompletedEventHandler((obj, args) =>
{
if (args.Error != null)
{
subject.OnError(args.Error);
}
else
{
if (!args.Cancelled)
{
var xdoc = XDocument.Load(args.Result);
subject.OnNext(xdoc.Root.Value);
}
subject.OnCompleted();
}
});
wc.OpenReadAsync(new Uri(detectUri));
return subject;
}
示例3: Search
public IObservable<TwitterSearchStatus> Search(string query, int page, int rpp)
{
var subject = new AsyncSubject<IEnumerable<TwitterSearchStatus>>();
this.service.Search(query, page, rpp, (tr, res) =>
{
if (res.InnerException == null)
{
subject.OnNext(tr.Statuses);
subject.OnCompleted();
}
else
{
subject.OnError(res.InnerException);
}
});
return subject.AsObservable().SelectMany(r => r);
}
示例4: LoginAsObservable
/// <summary>
/// WebContextのログインを行う。
/// </summary>
/// <param name="self"></param>
/// <param name="userName">ユーザ名</param>
/// <param name="password">パスワード</param>
/// <returns>ユーザ情報。ログインできていない場合はnull。</returns>
public static IObservable<User> LoginAsObservable(this WebContext self, string userName, string password)
{
// Subscribeまで処理を遅延させるためDeferでくるむ
return Observable.Defer(() =>
{
var async = new AsyncSubject<User>();
// ログインを行う
var op = self.Authentication.Login(userName, password);
// Completedイベントを購読して
Observable.FromEvent<EventHandler, EventArgs>(
h => (s, e) => h(e),
h => op.Completed += h,
h => op.Completed -= h)
// 1回のイベント発火でイベントハンドラを解除する
.Take(1)
.Subscribe(_ =>
{
// キャンセルされてたら何もせず終了
if (op.IsCanceled)
{
async.OnCompleted();
return;
}
// エラーの場合はエラー通知
if (op.HasError)
{
op.MarkErrorAsHandled();
async.OnError(op.Error);
return;
}
// ユーザ情報を発行して終了。
async.OnNext(op.User as User);
async.OnCompleted();
});
return async.AsObservable();
});
}
示例5: HasObservers_OnError
public void HasObservers_OnError()
{
var s = new AsyncSubject<int>();
Assert.IsFalse(s.HasObservers);
var d = s.Subscribe(_ => { }, ex => { });
Assert.IsTrue(s.HasObservers);
s.OnNext(42);
Assert.IsTrue(s.HasObservers);
s.OnError(new Exception());
Assert.IsFalse(s.HasObservers);
}
示例6: GetResult_Blocking_Throw
private void GetResult_Blocking_Throw(AsyncSubject<int> s)
{
Assert.IsFalse(s.IsCompleted);
var e = new ManualResetEvent(false);
var ex = new Exception();
new Thread(() => { e.WaitOne(); s.OnError(ex); }).Start();
var y = default(Exception);
var t = new Thread(() =>
{
try
{
s.GetResult();
}
catch (Exception ex_)
{
y = ex_;
}
});
t.Start();
while (t.ThreadState != ThreadState.WaitSleepJoin)
;
e.Set();
t.Join();
Assert.AreSame(ex, y);
Assert.IsTrue(s.IsCompleted);
}
示例7: AsyncSubjectTest
public void AsyncSubjectTest()
{
// OnCompletedPattern
{
var subject = new AsyncSubject<int>();
var onNext = new List<int>();
var exception = new List<Exception>();
int onCompletedCallCount = 0;
subject.Subscribe(x => onNext.Add(x), x => exception.Add(x), () => onCompletedCallCount++);
subject.OnNext(1);
subject.OnNext(10);
subject.OnNext(100);
subject.OnNext(1000);
onNext.Count.Is(0);
subject.OnCompleted();
onNext.IsCollection(1000);
onCompletedCallCount.Is(1);
subject.OnNext(1);
subject.OnNext(10);
subject.OnNext(100);
onNext.Count.Is(1);
subject.OnCompleted();
subject.OnError(new Exception());
exception.Count.Is(0);
onCompletedCallCount.Is(1);
// ++subscription
subject.Subscribe(x => onNext.Add(x), x => exception.Add(x), () => onCompletedCallCount++);
onNext.IsCollection(1000, 1000);
exception.Count.Is(0);
onCompletedCallCount.Is(2);
}
// OnErrorPattern
{
var subject = new AsyncSubject<int>();
var onNext = new List<int>();
var exception = new List<Exception>();
int onCompletedCallCount = 0;
subject.Subscribe(x => onNext.Add(x), x => exception.Add(x), () => onCompletedCallCount++);
subject.OnNext(1);
subject.OnNext(10);
subject.OnNext(100);
subject.OnNext(1000);
onNext.Count.Is(0);
subject.OnError(new Exception());
exception.Count.Is(1);
subject.OnNext(1);
subject.OnNext(10);
subject.OnNext(100);
onNext.Count.Is(0);
subject.OnCompleted();
subject.OnError(new Exception());
exception.Count.Is(1);
onCompletedCallCount.Is(0);
// ++subscription
subject.Subscribe(x => onNext.Add(x), x => exception.Add(x), () => onCompletedCallCount++);
onNext.Count.Is(0);
exception.Count.Is(2);
onCompletedCallCount.Is(0);
}
}
示例8: sends_no_values_after_error
public void sends_no_values_after_error()
{
AsyncSubject<int> subject = new AsyncSubject<int>();
StatsObserver<int> stats = new StatsObserver<int>();
subject.OnNext(1);
subject.OnNext(2);
subject.OnNext(3);
subject.OnError(new Exception());
subject.Subscribe(stats);
Assert.IsFalse(stats.NextCalled);
Assert.IsFalse(stats.CompletedCalled);
}
示例9: EstablishEncryptionAsync
public virtual IObservable<bool> EstablishEncryptionAsync()
{
ValidateDisposed();
var future = new AsyncSubject<bool>();
var watchStatusChanged = new SingleAssignmentDisposable();
watchStatusChanged.Disposable = this.ObserveStatusChanged().Subscribe(x =>
{
if (x == StatusCode.EncryptionFailedToEstablish)
{
watchStatusChanged.Dispose();
future.OnError(new EncryptionFailedToEstablishException());
}
if (x == StatusCode.EncryptionEstablished)
{
watchStatusChanged.Dispose();
future.OnNext(true);
future.OnCompleted();
}
});
if (this.EstablishEncryption())
{
return future.Timeout(Timeout).Catch((Exception ex) =>
{
watchStatusChanged.Dispose();
this.Disconnect();
return Observable.Throw<bool>(ex);
});
}
else
{
watchStatusChanged.Dispose();
return Observable.Return(false);
}
}
示例10: ConnectAsync
public virtual IObservable<bool> ConnectAsync(string serverAddress, string applicationName)
{
ValidateDisposed();
var future = new AsyncSubject<bool>();
var watchStatusChanged = new SingleAssignmentDisposable();
watchStatusChanged.Disposable = this.ObserveStatusChanged().Subscribe(x =>
{
if (x == StatusCode.ExceptionOnConnect)
{
watchStatusChanged.Dispose();
future.OnError(new ConnectionFailedException(serverAddress, applicationName));
}
if (x == StatusCode.Connect)
{
watchStatusChanged.Dispose();
future.OnNext(true);
future.OnCompleted();
}
});
if (this.Connect(serverAddress, applicationName))
{
this.LastConnectServerAddress = serverAddress;
this.LastConnectApplicationName = applicationName;
return future.Timeout(Timeout).Catch((Exception ex) =>
{
watchStatusChanged.Dispose();
this.Disconnect();
return Observable.Throw<bool>(ex);
});
}
else
{
watchStatusChanged.Dispose();
return Observable.Return(false);
}
}
示例11: QueueBackgroundDownloads
public IObservable<Unit> QueueBackgroundDownloads(IEnumerable<string> urls, IEnumerable<string> localPaths)
{
var ret = new AsyncSubject<Unit>();
var jobFiles = urls.Zip(localPaths, (url, path) => new {url, path});
var job = manager.CreateJob(applicationName + "_" + Guid.NewGuid() , JobType.Download);
job.OnJobError += (o, e) => ret.OnError(new BitsException(e.Error));
job.OnJobTransferred += (o, e) => {
try {
job.Complete();
if (job.ErrorCount != 0) {
ret.OnError(new BitsException(job.Error));
} else {
ret.OnNext(Unit.Default);
ret.OnCompleted();
}
} catch (Exception ex) {
ret.OnError(ex);
}
};
jobFiles
.Where(x => !File.Exists(x.path))
.ForEach(x => job.AddFile(x.url, x.path));
job.Resume();
return ret;
}
示例12: ResolveHostName
public static IObservable<NameResolutionResult> ResolveHostName(DnsEndPoint endPoint, NetworkInterfaceInfo networkInterface)
{
Contract.Requires(endPoint != null);
Contract.Requires(networkInterface != null);
Contract.Ensures(Contract.Result<IObservable<NameResolutionResult>>() != null);
var subject = new AsyncSubject<NameResolutionResult>();
try
{
DeviceNetworkInformation.ResolveHostNameAsync(
endPoint,
networkInterface,
result =>
{
var s = (AsyncSubject<NameResolutionResult>) result.AsyncState;
s.OnNext(result);
s.OnCompleted();
},
subject);
}
catch (Exception ex)
{
subject.OnError(ex);
}
return subject.AsObservable();
}