A cross-platform UI framework for .NET
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

34 lines
927 B

using System;
using System.Threading;
using System.Threading.Tasks;
namespace Avalonia.UnitTests;
public class ThreadRunHelper
{
public static Task<T> RunOnDedicatedThread<T>(Func<T> cb)
{
// Task.Run(...).GetAwaiter().GetResult() can be inlined, so we have this cursed thing instead
var tcs = new TaskCompletionSource<T>(TaskCreationOptions.RunContinuationsAsynchronously);
new Thread(() =>
{
try
{
tcs.SetResult(cb());
}
catch (Exception e)
{
tcs.SetException(e);
}
}).Start();
return tcs.Task;
}
public static Task RunOnDedicatedThread(Action cb) => RunOnDedicatedThread<object?>(() =>
{
cb();
return null;
});
public static void RunOnDedicatedThreadAndWait(Action cb) => RunOnDedicatedThread(cb).GetAwaiter().GetResult();
}