Share via


BC42358: Bu çağrı beklenmediği için, çağrı tamamlanmadan önce geçerli yöntemin yürütülmesi devam eder

Bu çağrı beklenmediği için, çağrı tamamlanmadan önce geçerli yöntemin yürütülmesi devam eder. çağrının Await sonucuna işlecini uygulamayı göz önünde bulundurun.

Geçerli yöntem, bir veya döndüren Task zaman uyumsuz bir yöntemi çağırır ve sonucta Await işlecini uygulamaz.Task<TResult> Zaman uyumsuz yöntem çağrısı zaman uyumsuz bir görev başlatır. Ancak hiçbir Await işleç uygulanmadığından, program görevin tamamlanmasını beklemeden devam eder. Çoğu durumda bu davranış beklenmez. Genellikle çağırma yönteminin diğer yönleri çağrının sonuçlarına bağlıdır veya çağrıyı içeren yöntemden dönmeden önce çağrılan yöntemin en az düzeyde tamamlanması beklenir.

Eşit derecede önemli bir sorun, çağrılan zaman uyumsuz yöntemde ortaya çıkan özel durumlarda ne olduğudur. veya Task<TResult> döndüren Task bir yöntemde tetiklenen ve döndürülen görevde depolanan bir özel durum. Görevi beklemezseniz veya özel durumları açıkça denetlemezseniz, özel durum kaybolur. Görevi beklerseniz, özel durumu yeniden oluşturulur.

En iyi uygulama olarak, aramayı her zaman beklemelisiniz.

Varsayılan olarak, bu ileti bir uyarıdır. Uyarıları gizleme veya uyarıları hata olarak işleme hakkında daha fazla bilgi için bkz . Visual Basic'te Uyarıları Yapılandırma.

Hata Kimliği: BC42358

Bu uyarıyı gidermek için

Yalnızca zaman uyumsuz çağrının tamamlanmasını beklemek istemediğinizden ve çağrılan yöntemin özel durum oluşturmayacağından eminseniz uyarıyı gizlemeyi düşünmelisiniz. Bu durumda, çağrının görev sonucunu bir değişkene atayarak uyarıyı gizleyebilirsiniz.

Aşağıdaki örnekte uyarıya nasıl neden olabileceğiniz, nasıl gizlenecekleri ve çağrının nasıl beklenir gösterilmektedir:

Async Function CallingMethodAsync() As Task

    ResultsTextBox.Text &= vbCrLf & "  Entering calling method."

    ' Variable delay is used to slow down the called method so that you
    ' can distinguish between awaiting and not awaiting in the program's output.
    ' You can adjust the value to produce the output that this topic shows
    ' after the code.
    Dim delay = 5000

    ' Call #1.
    ' Call an async method. Because you don't await it, its completion isn't
    ' coordinated with the current method, CallingMethodAsync.
    ' The following line causes the warning.
    CalledMethodAsync(delay)

    ' Call #2.
    ' To suppress the warning without awaiting, you can assign the
    ' returned task to a variable. The assignment doesn't change how
    ' the program runs. However, the recommended practice is always to
    ' await a call to an async method.
    ' Replace Call #1 with the following line.
    'Task delayTask = CalledMethodAsync(delay)

    ' Call #3
    ' To contrast with an awaited call, replace the unawaited call
    ' (Call #1 or Call #2) with the following awaited call. The best
    ' practice is to await the call.

    'Await CalledMethodAsync(delay)

    ' If the call to CalledMethodAsync isn't awaited, CallingMethodAsync
    ' continues to run and, in this example, finishes its work and returns
    ' to its caller.
    ResultsTextBox.Text &= vbCrLf & "  Returning from calling method."
End Function

Async Function CalledMethodAsync(howLong As Integer) As Task

    ResultsTextBox.Text &= vbCrLf & "    Entering called method, starting and awaiting Task.Delay."
    ' Slow the process down a little so you can distinguish between awaiting
    ' and not awaiting. Adjust the value for howLong if necessary.
    Await Task.Delay(howLong)
    ResultsTextBox.Text &= vbCrLf & "    Task.Delay is finished--returning from called method."
End Function

Örnekte, Çağrı #1 veya Çağrı #2'yi seçerseniz, çağrılmamış zaman uyumsuz yöntem (CalledMethodAsync) hem çağıranın () hem de çağıranın (CallingMethodAsyncStartButton_Click) tamamlanmasının ardından tamamlanır. Aşağıdaki çıktıdaki son satırda, çağrılan yöntem tamamlandığında gösterilir. Tam örnekte çağıran CallingMethodAsync olay işleyicisine giriş ve çıkış çıkışında işaretlenir.

Entering the Click event handler.
  Entering calling method.
    Entering called method, starting and awaiting Task.Delay.
  Returning from calling method.
Exiting the Click event handler.
    Task.Delay is finished--returning from called method.

Örnek

Aşağıdaki Windows Presentation Foundation (WPF) uygulaması önceki örnekteki yöntemleri içerir. Aşağıdaki adımlar uygulamayı ayarlar:

  1. BIR WPF uygulaması oluşturun ve adını verin AsyncWarning.

  2. Visual Studio Code Düzenleyicisi'nde MainWindow.xaml sekmesini seçin.

    Sekme görünmüyorsa, Çözüm Gezgini'de MainWindow.xaml kısayol menüsünü açın ve kodu görüntüle'yi seçin.

  3. MainWindow.xaml dosyasının XAML görünümündeki kodu aşağıdaki kodla değiştirin:

    <Window x:Class="MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="350" Width="525">
        <Grid>
            <Button x:Name="StartButton" Content="Start" HorizontalAlignment="Left" Margin="214,28,0,0" VerticalAlignment="Top" Width="75" HorizontalContentAlignment="Center" FontWeight="Bold" FontFamily="Aharoni" Click="StartButton_Click" />
            <TextBox x:Name="ResultsTextBox" Margin="0,80,0,0" TextWrapping="Wrap" FontFamily="Lucida Console"/>
        </Grid>
    </Window>
    

    MainWindow.xaml dosyasının Tasarım görünümünde düğme ve metin kutusu içeren basit bir pencere görüntülenir.

    XAML Tasarım Aracı hakkında daha fazla bilgi için bkz. XAML Tasarım Aracı kullanarak kullanıcı arabirimi oluşturma. Kendi basit kullanıcı arabiriminizi oluşturma hakkında bilgi için İzlenecek Yol: Async ve Await Kullanarak Web'e Erişme makalesinin "WPF uygulaması oluşturmak için" ve "Basit bir WPF MainWindow tasarlamak için" bölümlerine bakın.

  4. MainWindow.xaml.vb içindeki kodu aşağıdaki kodla değiştirin.

    Class MainWindow
    
        Private Async Sub StartButton_Click(sender As Object, e As RoutedEventArgs)
    
            ResultsTextBox.Text &= vbCrLf & "Entering the Click event handler."
            Await CallingMethodAsync()
            ResultsTextBox.Text &= vbCrLf & "Exiting the Click event handler."
        End Sub
    
        Async Function CallingMethodAsync() As Task
    
            ResultsTextBox.Text &= vbCrLf & "  Entering calling method."
    
            ' Variable delay is used to slow down the called method so that you
            ' can distinguish between awaiting and not awaiting in the program's output.
            ' You can adjust the value to produce the output that this topic shows
            ' after the code.
            Dim delay = 5000
    
            ' Call #1.
            ' Call an async method. Because you don't await it, its completion isn't
            ' coordinated with the current method, CallingMethodAsync.
            ' The following line causes the warning.
            CalledMethodAsync(delay)
    
            ' Call #2.
            ' To suppress the warning without awaiting, you can assign the
            ' returned task to a variable. The assignment doesn't change how
            ' the program runs. However, the recommended practice is always to
            ' await a call to an async method.
    
            ' Replace Call #1 with the following line.
            'Task delayTask = CalledMethodAsync(delay)
    
            ' Call #3
            ' To contrast with an awaited call, replace the unawaited call
            ' (Call #1 or Call #2) with the following awaited call. The best
            ' practice is to await the call.
    
            'Await CalledMethodAsync(delay)
    
            ' If the call to CalledMethodAsync isn't awaited, CallingMethodAsync
            ' continues to run and, in this example, finishes its work and returns
            ' to its caller.
            ResultsTextBox.Text &= vbCrLf & "  Returning from calling method."
        End Function
    
        Async Function CalledMethodAsync(howLong As Integer) As Task
    
            ResultsTextBox.Text &= vbCrLf & "    Entering called method, starting and awaiting Task.Delay."
            ' Slow the process down a little so you can distinguish between awaiting
            ' and not awaiting. Adjust the value for howLong if necessary.
            Await Task.Delay(howLong)
            ResultsTextBox.Text &= vbCrLf & "    Task.Delay is finished--returning from called method."
        End Function
    
    End Class
    
    ' Output
    
    ' Entering the Click event handler.
    '   Entering calling method.
    '     Entering called method, starting and awaiting Task.Delay.
    '   Returning from calling method.
    ' Exiting the Click event handler.
    '     Task.Delay is finished--returning from called method.
    
    ' Output
    
    ' Entering the Click event handler.
    '   Entering calling method.
    '     Entering called method, starting and awaiting Task.Delay.
    '     Task.Delay is finished--returning from called method.
    '   Returning from calling method.
    ' Exiting the Click event handler.
    
  5. Programı çalıştırmak için F5 tuşunu seçin ve ardından Başlat düğmesini seçin.

    Beklenen çıkış kodun sonunda görünür.

Ayrıca bkz.