/
githubmirror
/
aspnetcore
Обзор
Документация
Войти
/
githubmirror
/
aspnetcore
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
src/Shared/NonCapturingTimer/NonCapturingTimer.cs
41 строка
1 KB
James Newton-King
Enable throw helper analyzers (#45954)
10 янв 2023, 03:52
Не верифицирован
10 янв 2023, 03:52
c096dbb
Код
Авторство
О чём код?
// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. #nullable enable using System; using System.Threading; using Microsoft.AspNetCore.Shared; namespace Microsoft.Extensions.Internal; // A convenience API for interacting with System.Threading.Timer in a way // that doesn't capture the ExecutionContext. We should be using this (or equivalent) // everywhere we use timers to avoid rooting any values stored in asynclocals. internal static class NonCapturingTimer { public static Timer Create(TimerCallback callback, object? state, TimeSpan dueTime, TimeSpan period) { ArgumentNullThrowHelper.ThrowIfNull(callback); // Don't capture the current ExecutionContext and its AsyncLocals onto the timer bool restoreFlow = false; try { if (!ExecutionContext.IsFlowSuppressed()) { ExecutionContext.SuppressFlow(); restoreFlow = true; } return new Timer(callback, state, dueTime, period); } finally { // Restore the current ExecutionContext if (restoreFlow) { ExecutionContext.RestoreFlow(); } } } }