Created
August 1, 2012 13:16
-
-
Save ElemarJR/3226785 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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