当前位置: 首页 > 知识库问答 >
问题:

我是否应该担心“此异步方法缺少'await'运算符并将同步运行”警告

徐洋
2023-03-14

我知道我可以通过等待task.run来修复它,但对于只执行一些廉价操作的方法来说,这感觉不对。这听起来也会给执行增加不必要的开销,但我也不确定这是否已经存在,因为存在Async关键字。

我应该忽略这些警告,还是有一种我看不到的方法来解决这个问题?

共有1个答案

公孙芷阳
2023-03-14

只有当您实际调用所涉及的方法时,并且只有当性能是一个问题时。

可以通过编写一个包含以下4个方法的程序来演示这一点,然后将它们反编译为IL(请注意,在运行时版本之间,所提供的IL可能会发生变化;以下内容来自。NET Core3.1):

int FullySync() => 42;

Task<int> TaskSync() => Task.FromResult(42);

// CS1998
async Task<int> NotActuallyAsync() => 42;

async Task<int> FullyAsync() => await Task.Run(() => 42);

前两个会导致非常短的方法体,其中包含您所期望的内容:

.method private hidebysig 
    instance int32 FullySync () cil managed 
{
    // Method begins at RVA 0x205e
    // Code size 3 (0x3)
    .maxstack 8

    // return 42;
    IL_0000: ldc.i4.s 42
    IL_0002: ret
} // end of method Program::FullySync

.method private hidebysig 
    instance class [System.Runtime]System.Threading.Tasks.Task`1<int32> TaskSync () cil managed 
{
    // Method begins at RVA 0x2062
    // Code size 8 (0x8)
    .maxstack 8

    // return Task.FromResult(42);
    IL_0000: ldc.i4.s 42
    IL_0002: call class [System.Runtime]System.Threading.Tasks.Task`1<!!0> [System.Runtime]System.Threading.Tasks.Task::FromResult<int32>(!!0)
    IL_0007: ret
} // end of method Program::TaskSync
.method private hidebysig 
    instance class [System.Runtime]System.Threading.Tasks.Task`1<int32> NotActuallyAsync () cil managed 
{
    .custom instance void [System.Runtime]System.Runtime.CompilerServices.AsyncStateMachineAttribute::.ctor(class [System.Runtime]System.Type) = (
        01 00 29 43 53 31 39 39 38 54 65 73 74 2e 50 72
        6f 67 72 61 6d 2b 3c 4e 6f 74 41 63 74 75 61 6c
        6c 79 41 73 79 6e 63 3e 64 5f 5f 33 00 00
    )
    .custom instance void [System.Diagnostics.Debug]System.Diagnostics.DebuggerStepThroughAttribute::.ctor() = (
        01 00 00 00
    )
    // Method begins at RVA 0x206c
    // Code size 56 (0x38)
    .maxstack 2
    .locals init (
        [0] class CS1998Test.Program/'<NotActuallyAsync>d__3'
    )

    IL_0000: newobj instance void CS1998Test.Program/'<NotActuallyAsync>d__3'::.ctor()
    IL_0005: stloc.0
    IL_0006: ldloc.0
    IL_0007: call valuetype [System.Threading.Tasks]System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1<!0> valuetype [System.Threading.Tasks]System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1<int32>::Create()
    IL_000c: stfld valuetype [System.Threading.Tasks]System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1<int32> CS1998Test.Program/'<NotActuallyAsync>d__3'::'<>t__builder'
    IL_0011: ldloc.0
    IL_0012: ldarg.0
    IL_0013: stfld class CS1998Test.Program CS1998Test.Program/'<NotActuallyAsync>d__3'::'<>4__this'
    IL_0018: ldloc.0
    IL_0019: ldc.i4.m1
    IL_001a: stfld int32 CS1998Test.Program/'<NotActuallyAsync>d__3'::'<>1__state'
    IL_001f: ldloc.0
    IL_0020: ldflda valuetype [System.Threading.Tasks]System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1<int32> CS1998Test.Program/'<NotActuallyAsync>d__3'::'<>t__builder'
    IL_0025: ldloca.s 0
    IL_0027: call instance void valuetype [System.Threading.Tasks]System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1<int32>::Start<class CS1998Test.Program/'<NotActuallyAsync>d__3'>(!!0&)
    IL_002c: ldloc.0
    IL_002d: ldflda valuetype [System.Threading.Tasks]System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1<int32> CS1998Test.Program/'<NotActuallyAsync>d__3'::'<>t__builder'
    IL_0032: call instance class [System.Runtime]System.Threading.Tasks.Task`1<!0> valuetype [System.Threading.Tasks]System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1<int32>::get_Task()
    IL_0037: ret
} // end of method Program::NotActuallyAsync

.class nested private auto ansi sealed beforefieldinit '<NotActuallyAsync>d__3'
    extends [System.Runtime]System.Object
    implements [System.Runtime]System.Runtime.CompilerServices.IAsyncStateMachine
{
    .custom instance void [System.Runtime]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = (
        01 00 00 00
    )
    // Fields
    .field public int32 '<>1__state'
    .field public valuetype [System.Threading.Tasks]System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1<int32> '<>t__builder'
    .field public class CS1998Test.Program '<>4__this'

    // Methods
    .method public hidebysig specialname rtspecialname 
        instance void .ctor () cil managed 
    {
        // Method begins at RVA 0x20fd
        // Code size 8 (0x8)
        .maxstack 8

        // {
        IL_0000: ldarg.0
        // (no C# code)
        IL_0001: call instance void [System.Runtime]System.Object::.ctor()
        // }
        IL_0006: nop
        IL_0007: ret
    } // end of method '<NotActuallyAsync>d__3'::.ctor

    .method private final hidebysig newslot virtual 
        instance void MoveNext () cil managed 
    {
        .override method instance void [System.Runtime]System.Runtime.CompilerServices.IAsyncStateMachine::MoveNext()
        // Method begins at RVA 0x2108
        // Code size 58 (0x3a)
        .maxstack 2
        .locals init (
            [0] int32,
            [1] int32,
            [2] class [System.Runtime]System.Exception
        )

        // int num = <>1__state;
        IL_0000: ldarg.0
        IL_0001: ldfld int32 CS1998Test.Program/'<NotActuallyAsync>d__3'::'<>1__state'
        IL_0006: stloc.0
        .try
        {
            // result = 42;
            IL_0007: ldc.i4.s 42
            IL_0009: stloc.1
            // }
            IL_000a: leave.s IL_0024
        } // end .try
        catch [System.Runtime]System.Exception
        {
            // catch (Exception exception)
            IL_000c: stloc.2
            // <>1__state = -2;
            IL_000d: ldarg.0
            IL_000e: ldc.i4.s -2
            IL_0010: stfld int32 CS1998Test.Program/'<NotActuallyAsync>d__3'::'<>1__state'
            // <>t__builder.SetException(exception);
            IL_0015: ldarg.0
            IL_0016: ldflda valuetype [System.Threading.Tasks]System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1<int32> CS1998Test.Program/'<NotActuallyAsync>d__3'::'<>t__builder'
            IL_001b: ldloc.2
            IL_001c: call instance void valuetype [System.Threading.Tasks]System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1<int32>::SetException(class [System.Runtime]System.Exception)
            // return;
            IL_0021: nop
            IL_0022: leave.s IL_0039
        } // end handler

        // <>1__state = -2;
        IL_0024: ldarg.0
        IL_0025: ldc.i4.s -2
        IL_0027: stfld int32 CS1998Test.Program/'<NotActuallyAsync>d__3'::'<>1__state'
        // <>t__builder.SetResult(result);
        IL_002c: ldarg.0
        IL_002d: ldflda valuetype [System.Threading.Tasks]System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1<int32> CS1998Test.Program/'<NotActuallyAsync>d__3'::'<>t__builder'
        IL_0032: ldloc.1
        IL_0033: call instance void valuetype [System.Threading.Tasks]System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1<int32>::SetResult(!0)
        // }
        IL_0038: nop

        IL_0039: ret
    } // end of method '<NotActuallyAsync>d__3'::MoveNext

    .method private final hidebysig newslot virtual 
        instance void SetStateMachine (
            class [System.Runtime]System.Runtime.CompilerServices.IAsyncStateMachine stateMachine
        ) cil managed 
    {
        .custom instance void [System.Diagnostics.Debug]System.Diagnostics.DebuggerHiddenAttribute::.ctor() = (
            01 00 00 00
        )
        .override method instance void [System.Runtime]System.Runtime.CompilerServices.IAsyncStateMachine::SetStateMachine(class [System.Runtime]System.Runtime.CompilerServices.IAsyncStateMachine)
        // Method begins at RVA 0x2160
        // Code size 1 (0x1)
        .maxstack 8

        // }
        IL_0000: ret
    } // end of method '<NotActuallyAsync>d__3'::SetStateMachine

} // end of class <NotActuallyAsync>d__3

.method private hidebysig 
    instance class [System.Runtime]System.Threading.Tasks.Task`1<int32> FullyAsync () cil managed 
{
    .custom instance void [System.Runtime]System.Runtime.CompilerServices.AsyncStateMachineAttribute::.ctor(class [System.Runtime]System.Type) = (
        01 00 23 43 53 31 39 39 38 54 65 73 74 2e 50 72
        6f 67 72 61 6d 2b 3c 46 75 6c 6c 79 41 73 79 6e
        63 3e 64 5f 5f 34 00 00
    )
    .custom instance void [System.Diagnostics.Debug]System.Diagnostics.DebuggerStepThroughAttribute::.ctor() = (
        01 00 00 00
    )
    // Method begins at RVA 0x20b0
    // Code size 56 (0x38)
    .maxstack 2
    .locals init (
        [0] class CS1998Test.Program/'<FullyAsync>d__4'
    )

    IL_0000: newobj instance void CS1998Test.Program/'<FullyAsync>d__4'::.ctor()
    IL_0005: stloc.0
    IL_0006: ldloc.0
    IL_0007: call valuetype [System.Threading.Tasks]System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1<!0> valuetype [System.Threading.Tasks]System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1<int32>::Create()
    IL_000c: stfld valuetype [System.Threading.Tasks]System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1<int32> CS1998Test.Program/'<FullyAsync>d__4'::'<>t__builder'
    IL_0011: ldloc.0
    IL_0012: ldarg.0
    IL_0013: stfld class CS1998Test.Program CS1998Test.Program/'<FullyAsync>d__4'::'<>4__this'
    IL_0018: ldloc.0
    IL_0019: ldc.i4.m1
    IL_001a: stfld int32 CS1998Test.Program/'<FullyAsync>d__4'::'<>1__state'
    IL_001f: ldloc.0
    IL_0020: ldflda valuetype [System.Threading.Tasks]System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1<int32> CS1998Test.Program/'<FullyAsync>d__4'::'<>t__builder'
    IL_0025: ldloca.s 0
    IL_0027: call instance void valuetype [System.Threading.Tasks]System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1<int32>::Start<class CS1998Test.Program/'<FullyAsync>d__4'>(!!0&)
    IL_002c: ldloc.0
    IL_002d: ldflda valuetype [System.Threading.Tasks]System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1<int32> CS1998Test.Program/'<FullyAsync>d__4'::'<>t__builder'
    IL_0032: call instance class [System.Runtime]System.Threading.Tasks.Task`1<!0> valuetype [System.Threading.Tasks]System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1<int32>::get_Task()
    IL_0037: ret
} // end of method Program::FullyAsync

.class nested private auto ansi sealed beforefieldinit '<FullyAsync>d__4'
    extends [System.Runtime]System.Object
    implements [System.Runtime]System.Runtime.CompilerServices.IAsyncStateMachine
{
    .custom instance void [System.Runtime]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = (
        01 00 00 00
    )
    // Fields
    .field public int32 '<>1__state'
    .field public valuetype [System.Threading.Tasks]System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1<int32> '<>t__builder'
    .field public class CS1998Test.Program '<>4__this'
    .field private int32 '<>s__1'
    .field private valuetype [System.Runtime]System.Runtime.CompilerServices.TaskAwaiter`1<int32> '<>u__1'

    // Methods
    .method public hidebysig specialname rtspecialname 
        instance void .ctor () cil managed 
    {
        // Method begins at RVA 0x217b
        // Code size 8 (0x8)
        .maxstack 8

        // {
        IL_0000: ldarg.0
        // (no C# code)
        IL_0001: call instance void [System.Runtime]System.Object::.ctor()
        // }
        IL_0006: nop
        IL_0007: ret
    } // end of method '<FullyAsync>d__4'::.ctor

    .method private final hidebysig newslot virtual 
        instance void MoveNext () cil managed 
    {
        .override method instance void [System.Runtime]System.Runtime.CompilerServices.IAsyncStateMachine::MoveNext()
        // Method begins at RVA 0x2184
        // Code size 199 (0xc7)
        .maxstack 3
        .locals init (
            [0] int32,
            [1] int32,
            [2] valuetype [System.Runtime]System.Runtime.CompilerServices.TaskAwaiter`1<int32>,
            [3] class CS1998Test.Program/'<FullyAsync>d__4',
            [4] class [System.Runtime]System.Exception
        )

        // int num = <>1__state;
        IL_0000: ldarg.0
        IL_0001: ldfld int32 CS1998Test.Program/'<FullyAsync>d__4'::'<>1__state'
        IL_0006: stloc.0
        .try
        {
            // if (num != 0)
            IL_0007: ldloc.0
            IL_0008: brfalse.s IL_000c

            // (no C# code)
            IL_000a: br.s IL_000e

            // awaiter = Task.Run(() => 42).GetAwaiter();
            IL_000c: br.s IL_0065

            IL_000e: ldsfld class [System.Runtime]System.Func`1<int32> CS1998Test.Program/'<>c'::'<>9__4_0'
            IL_0013: dup
            IL_0014: brtrue.s IL_002d

            // (no C# code)
            IL_0016: pop
            // if (!awaiter.IsCompleted)
            IL_0017: ldsfld class CS1998Test.Program/'<>c' CS1998Test.Program/'<>c'::'<>9'
            IL_001c: ldftn instance int32 CS1998Test.Program/'<>c'::'<FullyAsync>b__4_0'()
            IL_0022: newobj instance void class [System.Runtime]System.Func`1<int32>::.ctor(object, native int)
            IL_0027: dup
            IL_0028: stsfld class [System.Runtime]System.Func`1<int32> CS1998Test.Program/'<>c'::'<>9__4_0'

            IL_002d: call class [System.Runtime]System.Threading.Tasks.Task`1<!!0> [System.Runtime]System.Threading.Tasks.Task::Run<int32>(class [System.Runtime]System.Func`1<!!0>)
            IL_0032: callvirt instance valuetype [System.Runtime]System.Runtime.CompilerServices.TaskAwaiter`1<!0> class [System.Runtime]System.Threading.Tasks.Task`1<int32>::GetAwaiter()
            IL_0037: stloc.2
            IL_0038: ldloca.s 2
            IL_003a: call instance bool valuetype [System.Runtime]System.Runtime.CompilerServices.TaskAwaiter`1<int32>::get_IsCompleted()
            IL_003f: brtrue.s IL_0081

            // num = (<>1__state = 0);
            IL_0041: ldarg.0
            IL_0042: ldc.i4.0
            IL_0043: dup
            IL_0044: stloc.0
            IL_0045: stfld int32 CS1998Test.Program/'<FullyAsync>d__4'::'<>1__state'
            // <>u__1 = awaiter;
            IL_004a: ldarg.0
            IL_004b: ldloc.2
            IL_004c: stfld valuetype [System.Runtime]System.Runtime.CompilerServices.TaskAwaiter`1<int32> CS1998Test.Program/'<FullyAsync>d__4'::'<>u__1'
            // <FullyAsync>d__4 stateMachine = this;
            IL_0051: ldarg.0
            IL_0052: stloc.3
            // <>t__builder.AwaitUnsafeOnCompleted(ref awaiter, ref stateMachine);
            IL_0053: ldarg.0
            IL_0054: ldflda valuetype [System.Threading.Tasks]System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1<int32> CS1998Test.Program/'<FullyAsync>d__4'::'<>t__builder'
            IL_0059: ldloca.s 2
            IL_005b: ldloca.s 3
            IL_005d: call instance void valuetype [System.Threading.Tasks]System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1<int32>::AwaitUnsafeOnCompleted<valuetype [System.Runtime]System.Runtime.CompilerServices.TaskAwaiter`1<int32>, class CS1998Test.Program/'<FullyAsync>d__4'>(!!0&, !!1&)
            // return;
            IL_0062: nop
            IL_0063: leave.s IL_00c6

            // awaiter = <>u__1;
            IL_0065: ldarg.0
            IL_0066: ldfld valuetype [System.Runtime]System.Runtime.CompilerServices.TaskAwaiter`1<int32> CS1998Test.Program/'<FullyAsync>d__4'::'<>u__1'
            IL_006b: stloc.2
            // <>u__1 = default(TaskAwaiter<int>);
            IL_006c: ldarg.0
            IL_006d: ldflda valuetype [System.Runtime]System.Runtime.CompilerServices.TaskAwaiter`1<int32> CS1998Test.Program/'<FullyAsync>d__4'::'<>u__1'
            IL_0072: initobj valuetype [System.Runtime]System.Runtime.CompilerServices.TaskAwaiter`1<int32>
            // num = (<>1__state = -1);
            IL_0078: ldarg.0
            IL_0079: ldc.i4.m1
            IL_007a: dup
            IL_007b: stloc.0
            IL_007c: stfld int32 CS1998Test.Program/'<FullyAsync>d__4'::'<>1__state'

            // <>s__1 = awaiter.GetResult();
            IL_0081: ldarg.0
            IL_0082: ldloca.s 2
            IL_0084: call instance !0 valuetype [System.Runtime]System.Runtime.CompilerServices.TaskAwaiter`1<int32>::GetResult()
            IL_0089: stfld int32 CS1998Test.Program/'<FullyAsync>d__4'::'<>s__1'
            // result = <>s__1;
            IL_008e: ldarg.0
            IL_008f: ldfld int32 CS1998Test.Program/'<FullyAsync>d__4'::'<>s__1'
            IL_0094: stloc.1
            // }
            IL_0095: leave.s IL_00b1
        } // end .try
        catch [System.Runtime]System.Exception
        {
            // catch (Exception exception)
            IL_0097: stloc.s 4
            // <>1__state = -2;
            IL_0099: ldarg.0
            IL_009a: ldc.i4.s -2
            IL_009c: stfld int32 CS1998Test.Program/'<FullyAsync>d__4'::'<>1__state'
            // <>t__builder.SetException(exception);
            IL_00a1: ldarg.0
            IL_00a2: ldflda valuetype [System.Threading.Tasks]System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1<int32> CS1998Test.Program/'<FullyAsync>d__4'::'<>t__builder'
            IL_00a7: ldloc.s 4
            IL_00a9: call instance void valuetype [System.Threading.Tasks]System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1<int32>::SetException(class [System.Runtime]System.Exception)
            // return;
            IL_00ae: nop
            IL_00af: leave.s IL_00c6
        } // end handler

        // <>1__state = -2;
        IL_00b1: ldarg.0
        IL_00b2: ldc.i4.s -2
        IL_00b4: stfld int32 CS1998Test.Program/'<FullyAsync>d__4'::'<>1__state'
        // <>t__builder.SetResult(result);
        IL_00b9: ldarg.0
        IL_00ba: ldflda valuetype [System.Threading.Tasks]System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1<int32> CS1998Test.Program/'<FullyAsync>d__4'::'<>t__builder'
        IL_00bf: ldloc.1
        IL_00c0: call instance void valuetype [System.Threading.Tasks]System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1<int32>::SetResult(!0)
        // }
        IL_00c5: nop

        IL_00c6: ret
    } // end of method '<FullyAsync>d__4'::MoveNext

    .method private final hidebysig newslot virtual 
        instance void SetStateMachine (
            class [System.Runtime]System.Runtime.CompilerServices.IAsyncStateMachine stateMachine
        ) cil managed 
    {
        .custom instance void [System.Diagnostics.Debug]System.Diagnostics.DebuggerHiddenAttribute::.ctor() = (
            01 00 00 00
        )
        .override method instance void [System.Runtime]System.Runtime.CompilerServices.IAsyncStateMachine::SetStateMachine(class [System.Runtime]System.Runtime.CompilerServices.IAsyncStateMachine)
        // Method begins at RVA 0x2268
        // Code size 1 (0x1)
        .maxstack 8

        // }
        IL_0000: ret
    } // end of method '<FullyAsync>d__4'::SetStateMachine

} // end of class <FullyAsync>d__4

您的情况基本上相反:您将一个方法定义为Async,但您知道并打算它不执行任何此类工作。但是编译器无法知道这一点--对html" target="_blank">编译器来说,它看起来与前面的情况完全相同,因此您得到了相同的警告。

老实说,在第二种情况下,您自己通过在实现中不必要地添加Async关键字而导致了警告。您知道该方法不执行任何异步工作,那么为什么还要添加关键字呢?你只是无缘无故地让它膨胀。

这个警告当然可以改进,以表明你基本上是在傻,我在罗斯林回购中打开了一个问题,希望能做到这一点。

 类似资料: