Aracılığıyla paylaş


Bu çağrı beklediğin değil çünkü geçerli yöntem çağrısı tamamlanmadan önce çalışmaya devam eder

Hata İletisi

Bu çağrı beklenmediğinden, çağrı tamamlanmadan geçerli yöntemin yürütülmesi devam eder.Arama sonucu için 'Await' işleci uygulamayı düşünmelidir.

Geçerli yöntemi döndüren bir zaman uyumsuz yöntemini çağırır bir Task veya bir Task<TResult> ve geçerli değildir Await sonucu işleci.Zaman uyumsuz yöntemine yapılan çağrının eşzamanlı olmayan bir görev başlatılır.Ancak, çünkü hiç Await program görevin tamamlanmasını beklemeden devam eder işleci uygulanır.Çoğu durumda, bu davranışı beklenen değildir.Genellikle arama sonuçları üzerinde diğer yönlerini çağıran yönteme bağlıdır veya çağrılan yöntemi çağrısı içeren yönteminden döndürmeden önce tamamlamak için gereken en düşük, bekleniyor.

Aynı derecede önemli bir sorun olarak adlandırılan zaman uyumsuz yöntem geçirilen istisnalar gerçekleşir.Döndüren bir yöntem geçirilen bir özel durum bir Task veya Task<TResult> döndürülen görevi depolanır.Görev beklemek veya açıkça denetlemek için özel durumlar yoksa, kayıp bir istisnadır.Görev beklemek, kendi özel durum rethrown.

En iyi yöntem, her zaman arama beklemek.

Varsayılan olarak, bu, bir uyarı iletisidir.Uyarılarını gizleme veya uyarıları hata olarak değerlendirmesini hakkında daha fazla bilgi için bkz: Visual Basic'te uyarılarını yapılandırma.

Hata kimliği: BC42358

Bu uyarıyı gidermek için

  • Yalnızca zaman uyumsuz çağrı tamamlamak beklemek istemiyorsanız ve Aranan yöntem istisnaları raise olmaz eminseniz uyarı engelleme göz önünde bulundurmalısınız.Bu durumda, görev sonucu çağrının bir değişkene atayarak uyarı bastırabilirsiniz.

    Aşağıdaki örnek, uyarı neden, onu bastırmak ve çağrı beklemek gösterir.

        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 Call # 1 veya # 2, unawaited zaman uyumsuz yöntem çağrısı seçerseniz, (CalledMethodAsync) sona erdikten sonra her iki arayan (CallingMethodAsync) ve arayanın arayan (StartButton_Click) tamamlandığında.Aranan Yöntem sonlandığında son satırın aşağıdaki çıktıyı göstermektedir.İçin giriş ve çıkış çağıran olay işleyicisi ' CallingMethodAsync tam örnek çıktı olarak işaretlenmiş.

    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 sunu Foundation (wpf) uygulaması, önceki örnekteki yöntemleri içerir.Aşağıdaki adımları uygulama ayarlama.

  1. wpf uygulaması oluşturup adlandırın AsyncWarning.

  2. Visual Studio Kod Düzenleyicisi'nde, seçim MainWindow.xaml sekmesi.

    Sekmesi görünmüyorsa, kısayol menüsünü açın MainWindow.xaml için Solution Explorerve sonra seçin View Code.

  3. Kod, yerini xaml MainWindow.xaml görünümü aşağıdaki kod ile.

    <Window x:Class="MainWindow"
            xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="https://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>
    

    Bir düğme ve metin kutusunu içeren basit bir pencere görünür Tasarım MainWindow.xaml görüntüleyin.

    xaml Tasarımcısı hakkında daha fazla bilgi için bkz: Creating a UI by using XAML Designer.Basit kendi kullanıcı Arabirimi oluşturma hakkında daha fazla bilgi için bkz: "wpf uygulama oluşturmak için" ve "tasarım basit wpf MainWindow" bölümlerine İzlenecek yol: zaman uyumsuz kullanarak Web'e erişme ve (C# ve Visual Basic) beklemek.

  4. MainWindow.xaml.vb 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. Seçim programını çalıştırın ve sonra seçmek için F5 tuşuna Start düğme.

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

Ayrıca bkz.

Başvuru

Beklemek işleci (Visual Basic)

Kavramlar

Zaman uyumsuz zaman uyumsuz programlama ve beklemek (C# ve Visual Basic)