本文整理匯總了C#中System.Threading.Tasks.Task.Catch方法的典型用法代碼示例。如果您正苦於以下問題:C# Task.Catch方法的具體用法?C# Task.Catch怎麽用?C# Task.Catch使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類System.Threading.Tasks.Task
的用法示例。
在下文中一共展示了Task.Catch方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。
示例1: TimerTicked
internal void TimerTicked()
{
if (currentTask != null)
return;
lock (this)
{
if (currentTask != null)
return;
if (DateTime.Now - lastRefresh < RefreshRate)
return;
currentTask = TimerTickedAsync();
if (currentTask == null)
return;
currentTask
.Catch()
.Finally(() =>
{
lastRefresh = DateTime.Now;
currentTask = null;
});
}
}
示例2: TimerTicked
internal void TimerTicked()
{
if (currentTask != null)
return;
lock (this)
{
if (currentTask != null)
return;
if (DateTime.Now - lastRefresh < GetRefreshRate())
return;
using(ServerModel.OnWebRequest(request => request.Headers["Raven-Timer-Request"] = "true"))
currentTask = TimerTickedAsync();
if (currentTask == null)
return;
currentTask
.Catch()
.Finally(() =>
{
lastRefresh = DateTime.Now;
IsForced = false;
currentTask = null;
});
}
}
示例3: TimerTicked
internal void TimerTicked()
{
if (ApplicationModel.Current.Server.Value.CreateNewDatabase)
{
ApplicationModel.Current.Server.Value.CreateNewDatabase = false;
ApplicationModel.Current.Server.Value.DocumentStore
.AsyncDatabaseCommands
.ForSystemDatabase()
.GetAsync("Raven/StudioConfig")
.ContinueOnSuccessInTheUIThread(doc =>
{
if (doc != null && doc.DataAsJson.ContainsKey("WarnWhenUsingSystemDatabase"))
{
if(doc.DataAsJson.Value<bool>("WarnWhenUsingSystemDatabase") == false)
return;
}
Command.ExecuteCommand(new CreateDatabaseCommand());
});
}
ApplicationModel.Current.UpdateAlerts();
if (currentTask != null)
return;
lock (this)
{
if (currentTask != null)
return;
var timeFromLastRefresh = SystemTime.UtcNow - lastRefresh;
var refreshRate = GetRefreshRate();
if (timeFromLastRefresh < refreshRate)
return;
using(OnWebRequest(request => request.Headers["Raven-Timer-Request"] = "true"))
currentTask = TimerTickedAsync();
if (currentTask == null)
return;
currentTask
.Catch()
.Finally(() =>
{
lastRefresh = SystemTime.UtcNow;
IsForced = false;
currentTask = null;
});
}
}
示例4: Catch_WithInputValue_IncompleteTaskOfFault_RunsOnNewThreadAndPostsToSynchronizationContext
public Task Catch_WithInputValue_IncompleteTaskOfFault_RunsOnNewThreadAndPostsToSynchronizationContext()
{
// Arrange
int outerThreadId = Thread.CurrentThread.ManagedThreadId;
int innerThreadId = Int32.MinValue;
Exception thrownException = new Exception();
Exception caughtException = null;
var syncContext = new Mock<SynchronizationContext> { CallBase = true };
SynchronizationContext.SetSynchronizationContext(syncContext.Object);
Task<int> incompleteTask = new Task<int>(() => { throw thrownException; });
// Act
Task<int> resultTask = incompleteTask.Catch(info =>
{
caughtException = info.Exception;
innerThreadId = Thread.CurrentThread.ManagedThreadId;
return info.Handled(42);
});
// Assert
incompleteTask.Start();
return resultTask.ContinueWith(task =>
{
Assert.Same(thrownException, caughtException);
Assert.NotEqual(innerThreadId, outerThreadId);
syncContext.Verify(sc => sc.Post(It.IsAny<SendOrPostCallback>(), null), Times.Once());
});
}
示例5: Catch_WithInputValue_IncompleteTaskOfSuccess_DoesNotRunContinuationAndDoesNotSwitchContexts
public Task Catch_WithInputValue_IncompleteTaskOfSuccess_DoesNotRunContinuationAndDoesNotSwitchContexts()
{
// Arrange
bool ranContinuation = false;
var syncContext = new Mock<SynchronizationContext> { CallBase = true };
SynchronizationContext.SetSynchronizationContext(syncContext.Object);
Task<int> incompleteTask = new Task<int>(() => 42);
// Act
Task<int> resultTask = incompleteTask.Catch(info =>
{
ranContinuation = true;
return info.Handled(42);
});
// Assert
incompleteTask.Start();
return resultTask.ContinueWith(task =>
{
Assert.False(ranContinuation);
syncContext.Verify(sc => sc.Post(It.IsAny<SendOrPostCallback>(), null), Times.Never());
});
}