Here are the examples of the csharp api System.Threading.Tasks.TaskCompletionSource.TrySetResult(TEventArgs) taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.
2 Examples
19
Source : AsyncExtensions.cs
with MIT License
from github-for-unity
with MIT License
from github-for-unity
public static async Task<TEventArgs> FromEvent<TEventHandler, TEventArgs>(
Func<Action<TEventArgs>, Action, Action<Exception>, TEventHandler> getHandler,
Action<TEventHandler> subscribe,
Action<TEventHandler> unsubscribe,
Action<Action<TEventArgs>, Action, Action<Exception>> initiate,
CancellationToken token = default(CancellationToken))
where TEventHandler : clreplaced
{
var tcs = new TaskCompletionSource<TEventArgs>();
Action<TEventArgs> complete = args => tcs.TrySetResult(args);
Action cancel = () => tcs.TrySetCanceled();
Action<Exception> reject = ex => tcs.TrySetException(ex);
TEventHandler handler = getHandler(complete, cancel, reject);
subscribe(handler);
try
{
using (token.Register(() => tcs.TrySetCanceled(),
useSynchronizationContext: false))
{
initiate(complete, cancel, reject);
return await tcs.Task;
}
}
finally
{
unsubscribe(handler);
}
}
19
Source : TaskExtensions.cs
with MIT License
from OpenXbox
with MIT License
from OpenXbox
public static async Task<TEventArgs> EventTask<T, TEventArgs>(
T obj,
Func<Task> postAddAction,
Action<T, EventHandler<TEventArgs>> add,
Action<T, EventHandler<TEventArgs>> remove,
Func<TEventArgs, bool> filter,
TimeSpan timeout)
where TEventArgs : EventArgs
{
var tcs = new TaskCompletionSource<TEventArgs>();
var timeoutCancellation = new CancellationTokenSource();
EventHandler<TEventArgs> handler = null;
handler = (s, e) =>
{
if (!filter(e))
{
return;
}
timeoutCancellation.Cancel();
tcs.TrySetResult(e);
remove(obj, handler);
};
add(obj, handler);
if (postAddAction != null)
{
await Task.Run(async () => await postAddAction());
}
Task.Delay(timeout, timeoutCancellation.Token).ContinueWith(t =>
{
if (timeoutCancellation.IsCancellationRequested)
{
return;
}
tcs.TrySetException(new TimeoutException());
remove(obj, handler);
});
return await tcs.Task;
}