mirror of
https://github.com/qmlnet/qmlnet.git
synced 2026-05-21 06:45:32 -06:00
Added DispatchAsync to QCoreApplication
Applications can now dispatch async task returning functions in addition to Actions
This commit is contained in:
parent
6f25ddfb89
commit
b7ff59629f
1 changed files with 28 additions and 0 deletions
|
|
@ -82,6 +82,34 @@ namespace Qml.Net
|
|||
RequestTrigger();
|
||||
}
|
||||
|
||||
public Task DispatchAsync(Func<Task> action)
|
||||
{
|
||||
var tcs = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
|
||||
using (var cts = new CancellationTokenSource(TimeSpan.FromSeconds(5)))
|
||||
{
|
||||
cts.Token.Register(tcs.SetCanceled);
|
||||
Dispatch(() =>
|
||||
{
|
||||
try
|
||||
{
|
||||
if (tcs.Task.IsCanceled)
|
||||
{
|
||||
return;
|
||||
}
|
||||
var task = action();
|
||||
task.ContinueWith((x) => tcs.SetResult(true), TaskContinuationOptions.NotOnFaulted);
|
||||
task.ContinueWith((x) => tcs.SetException(new OperationCanceledException()), TaskContinuationOptions.OnlyOnCanceled);
|
||||
task.ContinueWith((x) => tcs.SetException(x.Exception), TaskContinuationOptions.OnlyOnFaulted);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
tcs.SetException(ex);
|
||||
}
|
||||
});
|
||||
return tcs.Task;
|
||||
}
|
||||
}
|
||||
|
||||
public static void Exit(int returnCode = 0)
|
||||
{
|
||||
Interop.QCoreApplication.Exit(returnCode);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue