< Summary

Information
Class: dotnet_etcd.helper.AsyncHelper
Assembly: dotnet-etcd
File(s): /home/runner/work/dotnet-etcd/dotnet-etcd/dotnet-etcd/helper/AsyncHelper.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 12
Coverable lines: 12
Total lines: 47
Line coverage: 0%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.cctor()100%210%
RunSync(...)100%210%

File(s)

/home/runner/work/dotnet-etcd/dotnet-etcd/dotnet-etcd/helper/AsyncHelper.cs

#LineLine coverage
 1// Licensed to the .NET Foundation under one or more agreements.
 2// The .NET Foundation licenses this file to you under the MIT license.
 3
 4using System;
 5using System.Globalization;
 6using System.Threading;
 7using System.Threading.Tasks;
 8
 9namespace dotnet_etcd.helper;
 10
 11/// <summary>
 12///     Helps with running async code in sync methods
 13/// </summary>
 14/// <remarks>
 15///     Based on the AsyncHelper of the Microsoft.AspNet.Identity project
 16///     (https://web.archive.org/web/20200411071640/https://github.com/aspnet/AspNetIdentity/blob/master/src/Microsoft.A
 17/// </remarks>
 18public static class AsyncHelper
 19{
 020    private static readonly TaskFactory MyTaskFactory = new(CancellationToken.None,
 021        TaskCreationOptions.None, TaskContinuationOptions.None, TaskScheduler.Default);
 22
 23    /// <summary>
 24    ///     Runs the provided async function provided in <paramref name="func" /> in a synchronous way
 25    /// </summary>
 26    /// <param name="func">A call to the async method</param>
 27    /// <example>
 28    ///     <code>
 29    /// AsyncHelper.RunSync(SomeFunctionAsync)
 30    /// </code>
 31    ///     or
 32    ///     <code>
 33    /// AsyncHelper.RunSync(async () => await SomeFunctionAsync())
 34    /// </code>
 35    /// </example>
 36    public static void RunSync(Func<Task> func)
 037    {
 038        CultureInfo cultureUi = CultureInfo.CurrentUICulture;
 039        CultureInfo culture = CultureInfo.CurrentCulture;
 040        MyTaskFactory.StartNew(() =>
 041        {
 042            Thread.CurrentThread.CurrentCulture = culture;
 043            Thread.CurrentThread.CurrentUICulture = cultureUi;
 044            return func();
 045        }).Unwrap().GetAwaiter().GetResult();
 046    }
 47}