Repository: AvaloniaUI/Avalonia
File: src/Avalonia.Base/Threading/CulturePreservingExecutionContext.cs (C#)

Code before:
        {
            return null;
        }

        var context = ExecutionContext.Capture();
        if (context == null)
            return null;

        return new CulturePreservingExecutionContext(context);
    }

    /// <summary>
    /// Runs the specified callback in the captured execution context while preserving culture information.
    /// This method is used for .NET Framework and earlier .NET versions.
    /// </summary>
    /// <param name="executionContext">The execution context to run in.</param>
    /// <param name="callback">The callback to execute.</param>
    /// <param name="state">The state to pass to the callback.</param>
    public static void Run(CulturePreservingExecutionContext executionContext, ContextCallback callback, object? state)
    {
        // ReSharper disable once ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract
        if (callback == null)
            return;

        // ReSharper disable once ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract
        if (executionContext == null)
            ThrowNullContext();

        // Save culture information - we will need this to restore just before
        // the callback is actually invoked from CallbackWrapper.
        executionContext._cultureAndContext = CultureAndContext.Initialize(callback, state);

        try
        {
            ExecutionContext.Run(
                executionContext._context,
                s_callbackWrapperDelegate,
                executionContext._cultureAndContext);
        }
        finally
        {
            // Restore culture information - it might have been modified during callback execution.
            executionContext._cultureAndContext.RestoreCultureInfos();
        }
    }

    [DoesNotReturn]
#if NET6_0_OR_GREATER
    [StackTraceHidden]
#endif
    private static void ThrowNullContext()

Review comment (suggestion):
Since this version is "modernized", might as well make ContextCallback to be typed `ContextCallback<T>` to save on that cast later.

Code after:
    /// </summary>
    /// <returns>A new CulturePreservingExecutionContext instance, or null if no context needs to be captured.</returns>
    public static CulturePreservingExecutionContext? Capture()
    {
        // ExecutionContext.SuppressFlow had been called.
        // We expect ExecutionContext.Capture() to return null, so match that behavior and return null.
        if (ExecutionContext.IsFlowSuppressed())
        {
            return null;
        }

        var context = ExecutionContext.Capture();
        if (context == null)
            return null;

        return new CulturePreservingExecutionContext(context);
    }

    /// <summary>
    /// Runs the specified callback in the captured execution context while preserving culture information.
    /// This method is used for .NET Framework and earlier .NET versions.
    /// </summary>
    /// <param name="executionContext">The execution context to run in.</param>
    /// <param name="callback">The callback to execute.</param>
    /// <param name="state">The state to pass to the callback.</param>
    public static void Run(CulturePreservingExecutionContext executionContext, ContextCallback callback, object? state)
    {
        // ReSharper disable once ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract
        if (callback == null)
            return;

        // ReSharper disable once ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract
        if (executionContext == null)
            ThrowNullContext();

        // Save culture information - we will need this to restore just before
        // the callback is actually invoked from CallbackWrapper.
        executionContext._cultureAndContext = CultureAndContext.Initialize(callback, state);

        try
        {
            ExecutionContext.Run(
                executionContext._context,
                s_callbackWrapperDelegate,
                executionContext._cultureAndContext);
        }
        finally
        {
            // Restore culture information - it might have been modified during callback execution.
            executionContext._cultureAndContext.RestoreCultureInfos();
        }