本文整理汇总了C#中Task.CompletesAsyncOperation方法的典型用法代码示例。如果您正苦于以下问题:C# Task.CompletesAsyncOperation方法的具体用法?C# Task.CompletesAsyncOperation怎么用?C# Task.CompletesAsyncOperation使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Task
的用法示例。
在下文中一共展示了Task.CompletesAsyncOperation方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: StartModelUpdateAndSelectedItemUpdateTasks
/// <summary>
/// Starts a new task to compute the model based on the current text.
/// </summary>
private void StartModelUpdateAndSelectedItemUpdateTasks(int modelUpdateDelay, int selectedItemUpdateDelay, bool updateUIWhenDone)
{
AssertIsForeground();
var textSnapshot = _subjectBuffer.CurrentSnapshot;
var document = textSnapshot.GetOpenDocumentInCurrentContextWithChanges();
if (document == null)
{
return;
}
// Cancel off any existing work
_modelTaskCancellationSource.Cancel();
_modelTaskCancellationSource = new CancellationTokenSource();
var cancellationToken = _modelTaskCancellationSource.Token;
// Enqueue a new computation for the model
var asyncToken = _asyncListener.BeginAsyncOperation(GetType().Name + ".StartModelUpdateTask");
_modelTask =
Task.Delay(modelUpdateDelay, cancellationToken)
.SafeContinueWithFromAsync(
_ => ComputeModelAsync(document, textSnapshot, cancellationToken),
cancellationToken,
TaskContinuationOptions.OnlyOnRanToCompletion,
TaskScheduler.Default);
_modelTask.CompletesAsyncOperation(asyncToken);
StartSelectedItemUpdateTask(selectedItemUpdateDelay, updateUIWhenDone);
}
示例2: QueuedOperation
public void QueuedOperation()
{
using (var sleepHelper = new SleepHelper())
{
var signal = new ManualResetEventSlim();
var listener = new Listener();
var done = false;
var asyncToken1 = listener.BeginAsyncOperation("Test");
var task = new Task(() =>
{
signal.Set();
sleepHelper.Sleep(TimeSpan.FromMilliseconds(500));
var asyncToken2 = listener.BeginAsyncOperation("Test");
var queuedTask = new Task(() =>
{
sleepHelper.Sleep(TimeSpan.FromMilliseconds(500));
done = true;
});
queuedTask.CompletesAsyncOperation(asyncToken2);
queuedTask.Start();
});
task.CompletesAsyncOperation(asyncToken1);
task.Start();
Wait(listener, signal);
Assert.True(done, "Should have waited for the queued operation to finish!");
}
}
示例3: Operation
public void Operation()
{
using (var sleepHelper = new SleepHelper())
{
var signal = new ManualResetEventSlim();
var listener = new Listener();
var done = false;
var asyncToken = listener.BeginAsyncOperation("Test");
var task = new Task(() =>
{
signal.Set();
sleepHelper.Sleep(TimeSpan.FromSeconds(1));
done = true;
});
task.CompletesAsyncOperation(asyncToken);
task.Start();
Wait(listener, signal);
Assert.True(done, "The operation should have completed");
}
}
示例4: IgnoredCancel
public void IgnoredCancel()
{
using (var sleepHelper = new SleepHelper())
{
var signal = new ManualResetEventSlim();
var listener = new Listener();
var done = false;
var queuedFinished = false;
var cancelledFinished = false;
var asyncToken1 = listener.BeginAsyncOperation("Test");
var task = new Task(() =>
{
using (listener.BeginAsyncOperation("Test"))
{
var cancelledTask = new Task(() =>
{
sleepHelper.Sleep(TimeSpan.FromSeconds(10));
cancelledFinished = true;
});
signal.Set();
cancelledTask.Start();
}
sleepHelper.Sleep(TimeSpan.FromMilliseconds(500));
// Now that we've cancelled the first request, queue another one to make sure we wait for it.
var asyncToken2 = listener.BeginAsyncOperation("Test");
var queuedTask = new Task(() =>
{
sleepHelper.Sleep(TimeSpan.FromSeconds(1));
queuedFinished = true;
});
queuedTask.CompletesAsyncOperation(asyncToken2);
queuedTask.Start();
done = true;
});
task.CompletesAsyncOperation(asyncToken1);
task.Start();
Wait(listener, signal);
Assert.True(done, "Cancelling should have completed the current task.");
Assert.True(queuedFinished, "Continued didn't run, but it was supposed to ignore the cancel.");
Assert.False(cancelledFinished, "We waited for the cancelled task to finish.");
}
}
示例5: MultipleEnqueues
public void MultipleEnqueues()
{
using (var sleepHelper = new SleepHelper())
{
var signal = new ManualResetEventSlim();
var listener = new Listener();
var outerDone = false;
var firstQueuedDone = false;
var secondQueuedDone = false;
var asyncToken1 = listener.BeginAsyncOperation("Test");
var task = new Task(() =>
{
signal.Set();
sleepHelper.Sleep(TimeSpan.FromMilliseconds(500));
var asyncToken2 = listener.BeginAsyncOperation("Test");
var firstQueueTask = new Task(() =>
{
sleepHelper.Sleep(TimeSpan.FromMilliseconds(500));
var asyncToken3 = listener.BeginAsyncOperation("Test");
var secondQueueTask = new Task(() =>
{
sleepHelper.Sleep(TimeSpan.FromMilliseconds(500));
secondQueuedDone = true;
});
secondQueueTask.CompletesAsyncOperation(asyncToken3);
secondQueueTask.Start();
firstQueuedDone = true;
});
firstQueueTask.CompletesAsyncOperation(asyncToken2);
firstQueueTask.Start();
outerDone = true;
});
task.CompletesAsyncOperation(asyncToken1);
task.Start();
Wait(listener, signal);
Assert.True(outerDone, "The outer task should have finished!");
Assert.True(firstQueuedDone, "The first queued task should have finished");
Assert.True(secondQueuedDone, "The second queued task should have finished");
}
}
示例6: Nested
public void Nested()
{
using (var sleepHelper = new SleepHelper())
{
var signal = new ManualResetEventSlim();
var listener = new Listener();
var outerDone = false;
var innerDone = false;
var asyncToken1 = listener.BeginAsyncOperation("Test");
var task = new Task(() =>
{
signal.Set();
sleepHelper.Sleep(TimeSpan.FromMilliseconds(500));
using (listener.BeginAsyncOperation("Test"))
{
sleepHelper.Sleep(TimeSpan.FromMilliseconds(500));
innerDone = true;
}
sleepHelper.Sleep(TimeSpan.FromMilliseconds(500));
outerDone = true;
});
task.CompletesAsyncOperation(asyncToken1);
task.Start();
Wait(listener, signal);
Assert.True(innerDone, "Should have completed the inner task");
Assert.True(outerDone, "Should have completed the outer task");
}
}
示例7: Cancel
public void Cancel()
{
using (var sleepHelper = new SleepHelper())
{
var signal = new ManualResetEventSlim();
var listener = new Listener();
var done = false;
var continued = false;
var asyncToken1 = listener.BeginAsyncOperation("Test");
var task = new Task(() =>
{
signal.Set();
sleepHelper.Sleep(TimeSpan.FromMilliseconds(500));
var asyncToken2 = listener.BeginAsyncOperation("Test");
var queuedTask = new Task(() =>
{
sleepHelper.Sleep(TimeSpan.FromSeconds(5));
continued = true;
});
asyncToken2.Dispose();
queuedTask.Start();
done = true;
});
task.CompletesAsyncOperation(asyncToken1);
task.Start();
Wait(listener, signal);
Assert.True(done, "Cancelling should have completed the current task.");
Assert.False(continued, "Continued Task when it shouldn't have.");
}
}
示例8: UpdateConflictResolutionTask
private void UpdateConflictResolutionTask()
{
AssertIsForeground();
_conflictResolutionTaskCancellationSource.Cancel();
_conflictResolutionTaskCancellationSource = new CancellationTokenSource();
if (this.ReplacementText == string.Empty)
{
return;
}
var replacementText = this.ReplacementText;
var optionSet = _optionSet;
var cancellationToken = _conflictResolutionTaskCancellationSource.Token;
_conflictResolutionTask = _allRenameLocationsTask.SafeContinueWithFromAsync(
t => UpdateConflictResolutionTask(t.Result, replacementText, optionSet, cancellationToken),
cancellationToken,
TaskContinuationOptions.OnlyOnRanToCompletion,
TaskScheduler.Default);
var asyncToken = _asyncListener.BeginAsyncOperation("UpdateConflictResolutionTask");
_conflictResolutionTask.CompletesAsyncOperation(asyncToken);
}
示例9: StartSelectedItemUpdateTask
/// <summary>
/// Starts a new task to compute what item should be selected.
/// </summary>
private void StartSelectedItemUpdateTask(int delay, bool updateUIWhenDone)
{
AssertIsForeground();
var currentView = _presenter.TryGetCurrentView();
if (currentView == null)
{
return;
}
// Cancel off any existing work
_selectedItemInfoTaskCancellationSource.Cancel();
_selectedItemInfoTaskCancellationSource = new CancellationTokenSource();
var cancellationToken = _selectedItemInfoTaskCancellationSource.Token;
var subjectBufferCaretPosition = currentView.GetCaretPoint(_subjectBuffer);
if (!subjectBufferCaretPosition.HasValue)
{
return;
}
var asyncToken = _asyncListener.BeginAsyncOperation(GetType().Name + ".StartSelectedItemUpdateTask");
// Enqueue a new computation for the selected item
_selectedItemInfoTask = _modelTask.ContinueWithAfterDelay(
t => t.IsCanceled ? new NavigationBarSelectedTypeAndMember(null, null)
: ComputeSelectedTypeAndMember(t.Result, subjectBufferCaretPosition.Value, cancellationToken),
cancellationToken,
delay,
TaskContinuationOptions.None,
TaskScheduler.Default);
_selectedItemInfoTask.CompletesAsyncOperation(asyncToken);
if (updateUIWhenDone)
{
asyncToken = _asyncListener.BeginAsyncOperation(GetType().Name + ".StartSelectedItemUpdateTask.UpdateUI");
_selectedItemInfoTask.SafeContinueWith(
t => PushSelectedItemsToPresenter(t.Result),
cancellationToken,
TaskContinuationOptions.OnlyOnRanToCompletion,
ForegroundThreadAffinitizedObject.CurrentForegroundThreadData.TaskScheduler).CompletesAsyncOperation(asyncToken);
}
}