Condividi tramite


Annullamento: bridging tra .NET Framework e Windows Runtime (C# e Visual Basic)

È possibile ottimizzare le risorse combinando le funzionalità di .NET Framework e di Windows Runtime.L'esempio riportato in questo argomento viene illustrato come utilizzare un'istanza di .NET FrameworkCancellationToken per aggiungere un pulsante di annullamento in un'applicazione che utilizza il metodo di Windows Runtime per scaricare feed di blog dal web.

[!NOTA]

Per eseguire l'esempio, è necessario disporre di Windows 8 installato nel computer.Inoltre, se si desidera eseguire l'esempio da Visual Studio, è necessario disporre di Visual Studio 2012 o Visual Studio express 2012 per Windows 8 installato.

AsTask crea un ponte

Il token di annullamento richiede le istanze di Task, ma il metodo di Windows Runtime crea IAsyncOperationWithProgress le istanze.È possibile utilizzare il metodo di estensione AsTask in .NET Framework per integrare un ponte tra loro.

Il metodo di DownloadBlogsAsync nell'esempio è la maggior parte del lavoro.

Async Function DownloadBlogsAsync(ct As CancellationToken) As Task
    Dim client As Windows.Web.Syndication.SyndicationClient = New SyndicationClient()

    Dim uriList = CreateUriList()

    ' Force the SyndicationClient to download the information.
    client.BypassCacheOnRetrieve = True


    ' The following code avoids the use of implicit typing (var) so that you 
    ' can identify the types clearly.

    For Each uri In uriList
        ' ***These three lines are combined in the single statement that follows them.
        'Dim feedOp As IAsyncOperationWithProgress(Of SyndicationFeed, RetrievalProgress) =
        '    client.RetrieveFeedAsync(uri)
        'Dim feedTask As Task(Of SyndicationFeed) = feedOp.AsTask(ct)
        'Dim feed As SyndicationFeed = Await feedTask

        ' ***You can combine the previous three steps in one expression.
        Dim feed As SyndicationFeed = Await client.RetrieveFeedAsync(uri).AsTask(ct)

        DisplayResults(feed, ct)
    Next
End Function
async Task DownloadBlogsAsync(CancellationToken ct)
{
    Windows.Web.Syndication.SyndicationClient client = new SyndicationClient();

    var uriList = CreateUriList();

    // Force the SyndicationClient to download the information.
    client.BypassCacheOnRetrieve = true;

    // The following code avoids the use of implicit typing (var) so that you 
    // can identify the types clearly.

    foreach (var uri in uriList)
    {
        // ***These three lines are combined in the single statement that follows them.
        //IAsyncOperationWithProgress<SyndicationFeed, RetrievalProgress> feedOp = 
        //    client.RetrieveFeedAsync(uri);
        //Task<SyndicationFeed> feedTask = feedOp.AsTask(ct);
        //SyndicationFeed feed = await feedTask;

        // ***You can combine the previous three steps in one expression.
        SyndicationFeed feed = await client.RetrieveFeedAsync(uri).AsTask(ct);

        DisplayResults(feed);
    }
}

La sezione commentata - fuori del ciclo illustrata dettagliatamente i passaggi di transizione.

  • La chiamata SyndicationClient.RetrieveFeedAsync a viene avviata un'operazione asincrona che scarica un feed di blog da un URI specificato.L'operazione asincrona è IAsyncOperationWithProgress un'istanza di.

    Dim feedOp As IAsyncOperationWithProgress(Of SyndicationFeed, RetrievalProgress) = 
        client.RetrieveFeedAsync(uri)
    
    IAsyncOperationWithProgress<SyndicationFeed, RetrievalProgress> feedOp =  
        client.RetrieveFeedAsync(uri);
    
  • Poiché le funzionalità di annullamento in .NET Framework che si desidera utilizzare richiedono le attività, il codice consente di AsTask per rappresentare l'istanza di IAsyncOperationWithProgress come Task<TResult>.In particolare, il codice applica un overload di AsTask che accetta un argomento di CancellationToken.

    Dim feedTask As Task(Of SyndicationFeed) = feedOp.AsTask(ct)
    
    Task<SyndicationFeed> feedTask = feedOp.AsTask(ct);
    
  • Infine, await o l'operatore di Await attendere l'attività recuperare SyndicationFeed il risultato.

    Dim feed As SyndicationFeed = Await feedTask
    
    SyndicationFeed feed = await feedTask;
    

Per ulteriori informazioni su AsTask, vedere Estensione del codice iniziale di WhenAny: bridging tra .NET Framework e Windows Runtime (C# e Visual Basic).

Punti di interesse

È possibile rivedere l'intero esempio scorrendo alla fine di questo argomento, lo scaricare l'esempio nel computer locale, o compilare l'esempio.Per ulteriori informazioni e istruzioni, vedere Installazione di esempio.

Come esaminare l'esempio, si noterà gli asterischi che evidenziano gli aspetti importanti.È consigliabile avere letto questa sezione per comprendere questi punti, specialmente se si utilizza per la prima CancellationToken.

Per implementare un pulsante di annullamento, il codice deve includere gli elementi seguenti.

  • Una variabile di CancellationTokenSource, cts, che nell'ambito per tutti i metodi che accedono allo.

    Public NotInheritable Class MainPage
        Inherits Page
    
        ' ***Declare a System.Threading.CancellationTokenSource.
        Dim cts As CancellationTokenSource
    
    
    public sealed partial class MainPage : Page
    {
        // ***Declare a System.Threading.CancellationTokenSource.
        CancellationTokenSource cts;
    
  • Un gestore eventi per il pulsante Annulla.Il gestore eventi utilizza il metodo CancellationTokenSource.Cancel per notificare cts l'annullamento di richieste utente.

    ' ***Add an event handler for the Cancel button.
    Private Sub cancelButton_Click(sender As Object, e As RoutedEventArgs)
        If cts IsNot Nothing Then
            cts.Cancel()
            ResultsTextBox.Text &= vbCrLf & "Downloads canceled by the Cancel button."
        End If
    End Sub
    
    // ***Add an event handler for the Cancel button.
    private void CancelButton_Click(object sender, RoutedEventArgs e)
    {
        if (cts != null)
        {
            cts.Cancel();
            ResultsTextBox.Text += "\r\nDownloads canceled by the Cancel button.";
        }
    }
    
  • Un gestore eventi per il pulsante Avvio, StartButton_Click, incluse le azioni seguenti.

    • Il gestore eventi crea un'istanza di CancellationTokenSource, cts.

      cts = New CancellationTokenSource()
      
      // ***Instantiate the CancellationTokenSource.
      cts = new CancellationTokenSource();
      
    • Nella chiamata a DownloadBlogsAsync, che scarica feed di blog, il codice invia la proprietà di CancellationTokenSource.Token di cts come argomento.La proprietà Token propaga il messaggio se viene richiesto l'annullamento.

      Await DownloadBlogsAsync(cts.Token)
      
      await DownloadBlogsAsync(cts.Token);
      
    • La chiamata a DownloadBlogsAsync è alloggiata in un'istruzione di try-catch che include un blocco catch per OperationCanceledException che viene utilizzato quando si sceglie il pulsante Annulla.Il chiamante del metodo async definisce quale azione da eseguire.In questo esempio viene visualizzato un solo messaggio.

      Il codice seguente mostra l'istruzione completo try-catch.

      Try
          ' ***Send a token to carry the message if cancellation is requested.
          Await DownloadBlogsAsync(cts.Token)
      
          ' ***Check for cancellations.
      Catch op As OperationCanceledException
          ' In practice, this catch block often is empty. It is used to absorb
          ' the exception,
          ResultsTextBox.Text &= vbCrLf & "Cancellation exception bubbles up to the caller."
      
          ' Check for other exceptions.
      Catch ex As Exception
          ResultsTextBox.Text =
              "Page could not be loaded." & vbCrLf & "Exception: " & ex.ToString()
      End Try
      
      try
      {
          // ***Send a token to carry the message if cancellation is requested.
          await DownloadBlogsAsync(cts.Token);
      }
      // ***Check for cancellations.
      catch (OperationCanceledException)
      {
          // In practice, this catch block often is empty. It is used to absorb
          // the exception,
          ResultsTextBox.Text += "\r\nCancellation exception bubbles up to the caller.";
      }
      // Check for other exceptions.
      catch (Exception ex)
      {
          ResultsTextBox.Text =
              "Page could not be loaded.\r\n" + "Exception: " + ex.ToString();
      }
      
  • Come descritto precedentemente in questo argomento, le chiamate al metodo di DownloadBlogsAsync il metodo di Windows Runtime, RetrieveFeedAsynce applica un metodo di estensione .NET Framework, AsTask, all'istanza restituita di IAsyncOperation.AsTask rappresenta l'istanza di come Task, in modo da inviare il token di annullamento dell'operazione asincrona.Il token porta il messaggio quando si sceglie il pulsante Annulla.

    Si noti che tramite AsTask, il codice può passare la stessa istanza di CancellationToken sia a un metodo di Windows Runtime (RetrieveFeedAsync) che a un metodo di .NET Framework (DownloadBlogsAsync).

    La riga seguente viene illustrata questa parte del codice.

    Dim feed As SyndicationFeed = Await client.RetrieveFeedAsync(uri).AsTask(ct)
    
    SyndicationFeed feed = await client.RetrieveFeedAsync(uri).AsTask(ct);
    
  • Se non si annulla l'applicazione, produce l'output seguente.

    Developing for Windows
        New blog for Windows 8 app developers, 5/1/2012 2:33:02 PM -07:00
        Trigger-Start Services Recipe, 3/24/2011 2:23:01 PM -07:00
        Windows Restart and Recovery Recipe, 3/21/2011 2:13:24 PM -07:00
    
    Extreme Windows Blog
        Samsung Series 9 27” PLS Display: Amazing Picture, 8/20/2012 2:41:48 PM -07:00
        NVIDIA GeForce GTX 660 Ti Graphics Card: Affordable Graphics Powerhouse, 8/16/2012 10:56:19 AM -07:00
        HP Z820 Workstation: Rising To the Challenge, 8/14/2012 1:57:01 PM -07:00
    
    Blogging Windows
        Windows Upgrade Offer Registration Now Available, 8/20/2012 1:01:00 PM -07:00
        Windows 8 has reached the RTM milestone, 8/1/2012 9:00:00 AM -07:00
        Windows 8 will be available on…, 7/18/2012 1:09:00 PM -07:00
    
    Windows for your Business
        What Windows 8 RTM Means for Businesses, 8/1/2012 9:01:00 AM -07:00
        Higher-Ed Learning with Windows 8, 7/26/2012 12:03:00 AM -07:00
        Second Public Beta of App-V 5.0 Now Available with Office Integration, 7/24/2012 10:07:26 AM -07:00
    
    Windows Experience Blog
        Tech Tuesday Live Twitter Chat with Microsoft Hardware, 8/20/2012 2:20:57 AM -07:00
        New Colors and New Artist Series Mice from Microsoft Hardware, 8/15/2012 12:06:35 AM -07:00
        Tech Tuesday Live Twitter Chat with HP on Keeping Kids Safe as They Head Back to School #winchat, 8/13/2012 12:24:18 PM -07:00
    
    Windows Security Blog
        Dealing with Fake Tech Support & Phone Scams, 6/16/2011 1:53:00 PM -07:00
        Combating social engineering tactics, like cookiejacking, to stay safer online, 5/28/2011 12:02:26 PM -07:00
        Windows 7 is now Common Criteria Certified!, 4/27/2011 9:35:01 AM -07:00
    
    Windows Home Server Blog
        Connecting Windows 8 Consumer Preview with Windows Home Server, 3/25/2012 9:06:00 AM -07:00
        Viridian PC Systems announces two new server models are available to order, 10/3/2011 12:36:00 PM -07:00
        PC Specialist to release Windows Home Server 2011, 9/27/2011 10:27:37 AM -07:00
    
    Springboard Series Blog
        Windows 8 Is Ready For Your Enterprise, 8/16/2012 9:59:00 AM -07:00
        What to Expect in User Experience Virtualization Beta 2, 6/25/2012 11:03:27 PM -07:00
        Introducing Microsoft BitLocker Administration 2.0 Beta, 6/12/2012 8:08:23 AM -07:00
    

    Se si sceglie il pulsante Annulla prima che l'applicazione completi scaricare il contenuto, il risultato sarà simile al seguente output.

    Developing for Windows
        New blog for Windows 8 app developers, 5/1/2012 2:33:02 PM -07:00
        Trigger-Start Services Recipe, 3/24/2011 2:23:01 PM -07:00
        Windows Restart and Recovery Recipe, 3/21/2011 2:13:24 PM -07:00
    
    Extreme Windows Blog
        Samsung Series 9 27” PLS Display: Amazing Picture, 8/20/2012 2:41:48 PM -07:00
        NVIDIA GeForce GTX 660 Ti Graphics Card: Affordable Graphics Powerhouse, 8/16/2012 10:56:19 AM -07:00
        HP Z820 Workstation: Rising To the Challenge, 8/14/2012 1:57:01 PM -07:00
    
    Blogging Windows
        Windows Upgrade Offer Registration Now Available, 8/20/2012 1:01:00 PM -07:00
        Windows 8 has reached the RTM milestone, 8/1/2012 9:00:00 AM -07:00
        Windows 8 will be available on…, 7/18/2012 1:09:00 PM -07:00
    
    Windows for your Business
        What Windows 8 RTM Means for Businesses, 8/1/2012 9:01:00 AM -07:00
        Higher-Ed Learning with Windows 8, 7/26/2012 12:03:00 AM -07:00
        Second Public Beta of App-V 5.0 Now Available with Office Integration, 7/24/2012 10:07:26 AM -07:00
    
    Downloads canceled by the Cancel button.
    Cancellation exception bubbles up to the caller.
    

Installazione di esempio

È possibile scaricare l'applicazione, essere compilato manualmente, o esaminare il codice alla fine di questo argomento senza distribuirlo.Visual Studio 2012 e Windows 8 devono essere installati nel computer per eseguire questa applicazione.

Per scaricare l'applicazione finita

  1. Scaricare il file appiattito da Esempio di Async: Gettando un ponte tra.NET e il runtime di Windows (& AsTask annullamento).

  2. Decomprimere il file che viene scaricato e quindi avviare Visual Studio.

  3. Sulla barra dei menu scegliere File, Apri, Progetto/Soluzione.

  4. Passare alla cartella che contiene il codice di esempio decompresso quindi aprire il file di soluzione (sln).

  5. Scegliere il tasto F5 per compilare ed eseguire il progetto.

    Eseguire il codice più volte per verificare che sia possibile annullare nei diversi.

Per compilare l'applicazione finita

  1. Avviare Visual Studio.

  2. Nella barra del menu, scegliere File, Nuovo, Progetto.

    Verrà visualizzata la finestra di dialogo Nuovo progetto.

  3. In Installato, la categoria Modelli, scegliere Visual Basic o Visual C#quinWindows Store.

  4. Nell'elenco di tipi di progetto, scegliere Applicazione vuota (XAML).

  5. Denominare il progetto BlogFeedWithCancellationquindi scegliere il pulsante OK.

    Il nuovo progetto verrà visualizzato in Esplora soluzioni.

  6. In Esplora soluzioni, scegliere dal menu di scelta rapida per MainPage.xaml quindi scegliere Apri.

  7. Nella finestra XAML del file MainPage.xaml, sostituire il codice con il codice seguente.

    <Page
        x:Class="BlogFeedWithCancellation.MainPage"
        xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="using:BlogFeedWithCancellation"
        xmlns:d="https://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="https://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d">
    
        <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
            <Button x:Name="StartButton" Content="Start" HorizontalAlignment="Left" Margin="325,77,0,0" VerticalAlignment="Top" Click="StartButton_Click" Height="145" Background="#FFA89B9B" FontSize="36" Width="355"  />
            <Button x:Name="CancelButton" Content="Cancel" HorizontalAlignment="Left" Margin="684,77,0,0" VerticalAlignment="Top" Height="145" Background="#FFA89B9B" Click="CancelButton_Click" FontSize="36" Width="355"  />
            <TextBox x:Name="ResultsTextBox" HorizontalAlignment="Left" Margin="325,222,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Height="546" FontSize="10" ScrollViewer.VerticalScrollBarVisibility="Visible" Width="711" />
        </Grid>
    </Page>
    

    Una finestra semplice che contiene una casella di testo, un pulsante di avvio e un pulsante di annullamento viene visualizzato nella finestra Progettazione del file MainPage.xaml.

  8. In Esplora soluzioni, scegliere dal menu di scelta rapida per MainPage.xaml.vb o MainPage.xaml.cs quindi scegliere Visualizza codice.

  9. Sostituire il codice in MainPage.xaml.vb o in MainPage.xaml.cs con il codice seguente.

    ' Add an Imports statement for SyndicationClient.
    Imports Windows.Web.Syndication
    ' Add an Imports statement for Tasks.
    Imports System.Threading.Tasks
    ' Add an Imports statement for CancellationToken.
    Imports System.Threading
    
    
    Public NotInheritable Class MainPage
        Inherits Page
    
        ' ***Declare a System.Threading.CancellationTokenSource.
        Dim cts As CancellationTokenSource
    
        Private Async Sub StartButton_Click(sender As Object, e As RoutedEventArgs)
            ResultsTextBox.Text = ""
            ' Prevent unexpected reentrance.
            StartButton.IsEnabled = False
    
            ' ***Instantiate the CancellationTokenSource.
            cts = New CancellationTokenSource()
    
            Try
                ' ***Send a token to carry the message if cancellation is requested.
                Await DownloadBlogsAsync(cts.Token)
    
                ' ***Check for cancellations.
            Catch op As OperationCanceledException
                ' In practice, this catch block often is empty. It is used to absorb
                ' the exception,
                ResultsTextBox.Text &= vbCrLf & "Cancellation exception bubbles up to the caller."
    
                ' Check for other exceptions.
            Catch ex As Exception
                ResultsTextBox.Text =
                    "Page could not be loaded." & vbCrLf & "Exception: " & ex.ToString()
            End Try
    
            ' ***Set the CancellationTokenSource to null when the work is complete.
            cts = Nothing
    
            ' In case you want to try again.
            StartButton.IsEnabled = True
        End Sub
    
    
        ' Provide a parameter for the CancellationToken.
        Async Function DownloadBlogsAsync(ct As CancellationToken) As Task
            Dim client As Windows.Web.Syndication.SyndicationClient = New SyndicationClient()
    
            Dim uriList = CreateUriList()
    
            ' Force the SyndicationClient to download the information.
            client.BypassCacheOnRetrieve = True
    
    
            ' The following code avoids the use of implicit typing (var) so that you 
            ' can identify the types clearly.
    
            For Each uri In uriList
                ' ***These three lines are combined in the single statement that follows them.
                'Dim feedOp As IAsyncOperationWithProgress(Of SyndicationFeed, RetrievalProgress) =
                '    client.RetrieveFeedAsync(uri)
                'Dim feedTask As Task(Of SyndicationFeed) = feedOp.AsTask(ct)
                'Dim feed As SyndicationFeed = Await feedTask
    
                ' ***You can combine the previous three steps in one expression.
                Dim feed As SyndicationFeed = Await client.RetrieveFeedAsync(uri).AsTask(ct)
    
                DisplayResults(feed, ct)
            Next
        End Function
    
    
        ' ***Add an event handler for the Cancel button.
        Private Sub cancelButton_Click(sender As Object, e As RoutedEventArgs)
            If cts IsNot Nothing Then
                cts.Cancel()
                ResultsTextBox.Text &= vbCrLf & "Downloads canceled by the Cancel button."
            End If
        End Sub
    
    
        Function CreateUriList() As List(Of Uri)
            ' Create a list of URIs.
            Dim uriList = New List(Of Uri) From
                    {
                        New Uri("https://windowsteamblog.com/windows/b/developers/atom.aspx"),
                        New Uri("https://windowsteamblog.com/windows/b/extremewindows/atom.aspx"),
                        New Uri("https://windowsteamblog.com/windows/b/bloggingwindows/atom.aspx"),
                        New Uri("https://windowsteamblog.com/windows/b/business/atom.aspx"),
                        New Uri("https://windowsteamblog.com/windows/b/windowsexperience/atom.aspx"),
                        New Uri("https://windowsteamblog.com/windows/b/windowssecurity/atom.aspx"),
                        New Uri("https://windowsteamblog.com/windows/b/windowshomeserver/atom.aspx"),
                        New Uri("https://windowsteamblog.com/windows/b/springboard/atom.aspx")
                    }
            Return uriList
        End Function
    
    
        ' You can pass the CancellationToken to this method if you think you might use a
        ' cancellable API here in the future.
        Sub DisplayResults(sf As SyndicationFeed, ct As CancellationToken)
            ' Title of the blog.
            ResultsTextBox.Text &= sf.Title.Text & vbCrLf
    
            ' Titles and dates for the first three blog posts.
            For i As Integer = 0 To If(sf.Items.Count >= 3, 2, sf.Items.Count)
                ResultsTextBox.Text &= vbTab & sf.Items.ElementAt(i).Title.Text & ", " &
                        sf.Items.ElementAt(i).PublishedDate.ToString() & vbCrLf
            Next
    
            ResultsTextBox.Text &= vbCrLf
        End Sub
    End Class
    
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using Windows.Foundation;
    using Windows.Foundation.Collections;
    using Windows.UI.Xaml;
    using Windows.UI.Xaml.Controls;
    using Windows.UI.Xaml.Controls.Primitives;
    using Windows.UI.Xaml.Data;
    using Windows.UI.Xaml.Input;
    using Windows.UI.Xaml.Media;
    using Windows.UI.Xaml.Navigation;
    
    // Add a using directive for SyndicationClient.
    using Windows.Web.Syndication;
    // Add a using directive for Tasks.
    using System.Threading.Tasks;
    // Add a using directive for CancellationToken.
    using System.Threading;
    
    
    namespace BlogFeedWithCancellation
    {
        public sealed partial class MainPage : Page
        {
            // ***Declare a System.Threading.CancellationTokenSource.
            CancellationTokenSource cts;
    
            public MainPage()
            {
                this.InitializeComponent();
            }
    
    
            private async void StartButton_Click(object sender, RoutedEventArgs e)
            {
                ResultsTextBox.Text = "";
                // Prevent unexpected reentrance.
                StartButton.IsEnabled = false;
    
                // ***Instantiate the CancellationTokenSource.
                cts = new CancellationTokenSource();
    
                try
                {
                    // ***Send a token to carry the message if cancellation is requested.
                    await DownloadBlogsAsync(cts.Token);
                }
                // ***Check for cancellations.
                catch (OperationCanceledException)
                {
                    // In practice, this catch block often is empty. It is used to absorb
                    // the exception,
                    ResultsTextBox.Text += "\r\nCancellation exception bubbles up to the caller.";
                }
                // Check for other exceptions.
                catch (Exception ex)
                {
                    ResultsTextBox.Text =
                        "Page could not be loaded.\r\n" + "Exception: " + ex.ToString();
                }
    
                // ***Set the CancellationTokenSource to null when the work is complete.
                cts = null;
    
                // In case you want to try again.
                StartButton.IsEnabled = true;
            }
    
    
            // ***Provide a parameter for the CancellationToken.
            async Task DownloadBlogsAsync(CancellationToken ct)
            {
                Windows.Web.Syndication.SyndicationClient client = new SyndicationClient();
    
                var uriList = CreateUriList();
    
                // Force the SyndicationClient to download the information.
                client.BypassCacheOnRetrieve = true;
    
                // The following code avoids the use of implicit typing (var) so that you 
                // can identify the types clearly.
    
                foreach (var uri in uriList)
                {
                    // ***These three lines are combined in the single statement that follows them.
                    //IAsyncOperationWithProgress<SyndicationFeed, RetrievalProgress> feedOp = 
                    //    client.RetrieveFeedAsync(uri);
                    //Task<SyndicationFeed> feedTask = feedOp.AsTask(ct);
                    //SyndicationFeed feed = await feedTask;
    
                    // ***You can combine the previous three steps in one expression.
                    SyndicationFeed feed = await client.RetrieveFeedAsync(uri).AsTask(ct);
    
                    DisplayResults(feed);
                }
            }
    
    
    
            // ***Add an event handler for the Cancel button.
            private void CancelButton_Click(object sender, RoutedEventArgs e)
            {
                if (cts != null)
                {
                    cts.Cancel();
                    ResultsTextBox.Text += "\r\nDownloads canceled by the Cancel button.";
                }
            }
    
    
            List<Uri> CreateUriList()
            {
                // Create a list of URIs.
                List<Uri> uriList = new List<Uri> 
                { 
                    new Uri("https://windowsteamblog.com/windows/b/developers/atom.aspx"),
                    new Uri("https://windowsteamblog.com/windows/b/extremewindows/atom.aspx"),
                    new Uri("https://windowsteamblog.com/windows/b/bloggingwindows/atom.aspx"),
                    new Uri("https://windowsteamblog.com/windows/b/business/atom.aspx"),
                    new Uri("https://windowsteamblog.com/windows/b/windowsexperience/atom.aspx"),
                    new Uri("https://windowsteamblog.com/windows/b/windowssecurity/atom.aspx"),
                    new Uri("https://windowsteamblog.com/windows/b/windowshomeserver/atom.aspx"),
                    new Uri("https://windowsteamblog.com/windows/b/springboard/atom.aspx")
                };
                return uriList;
            }
    
    
            // You can pass the CancellationToken to this method if you think you might use a
            // cancellable API here in the future.
            void DisplayResults(SyndicationFeed sf)
            {
                // Title of the blog.
                ResultsTextBox.Text += sf.Title.Text + "\r\n";
    
                // Titles and dates for the first three blog posts.
                for (int i = 0; i < (sf.Items.Count < 3 ? sf.Items.Count : 3); i++)    // Is Math.Min better?
                {
                    ResultsTextBox.Text += "\t" + sf.Items.ElementAt(i).Title.Text + ", " +
                        sf.Items.ElementAt(i).PublishedDate.ToString() + "\r\n";
                }
    
                ResultsTextBox.Text += "\r\n";
            }
        }
    }
    
  10. Premere il tasto F5 per eseguire il programma, quindi scegliere il pulsante Avvia.

Vedere anche

Concetti

WhenAny: bridging tra .NET Framework e Windows Runtime (C# e Visual Basic)

Annullare un'attività o un elenco di attività (C# e Visual Basic)

Annullare attività dopo un periodo di tempo (C# e Visual Basic)

Annullare le attività rimanenti dopo che ne è stata completata una (C# e Visual Basic)

Token di annullamento