Skip to content

Instantly share code, notes, and snippets.

@ElemarJR
Created August 1, 2012 13:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ElemarJR/3226785 to your computer and use it in GitHub Desktop.
Save ElemarJR/3226785 to your computer and use it in GitHub Desktop.
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void StartButtonClick(object sender, RoutedEventArgs e)
{
this.Update();
}
private void StopButtonClick(object sender, RoutedEventArgs e)
{
cts.Cancel();
}
CancellationTokenSource cts;
async void Update()
{
try
{
this.StartButton.IsEnabled = false;
this.StopButton.IsEnabled = true;
cts = new CancellationTokenSource();
this.output.ItemsSource = null;
this.output.ItemsSource = await GetLinksFromMsdnAsync();
}
catch (TaskCanceledException)
{
MessageBox.Show("Cancelled!");
}
finally
{
this.StartButton.IsEnabled = true;
this.StopButton.IsEnabled = false;
}
}
async Task<IEnumerable<string>> GetLinksFromMsdnAsync()
{
var response = await new WebClient()
.DownloadStringTaskAsync(
new Uri("http://msdn.microsoft.com/pt-br/"),
cts.Token
);
var regex = "href\\s*=\\s*(?:\"(?<1>[^\"]*)\"|(?<1>\\S+))";
return Regex.Matches(response, regex)
.OfType<Match>()
.Select(m => m.Groups[1].Value);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment