Created
November 12, 2012 14:30
-
-
Save rarous/4059724 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| query.Subscribe(x => | |
| { | |
| Task.Factory.StartNew(() => | |
| { | |
| if (IsAvailable(x)) | |
| proxyQueue.Enqueue(x); | |
| }, TaskCreationOptions.LongRunning); | |
| }); |
Snad jsme tě pochopil, ten kód by šel samozřejmě vylepšit přes RXX
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Reactive;
using System.Text;
using System.Reactive.Linq;
namespace AlesRX
{
class Program
{
static void Main(string[] args)
{
var proxyQueue = new ConcurrentQueue<Uri>();
var query = new List<Uri>
{
new Uri("http://twitter.com"),
new Uri("http://google.com"),
new Uri("http://alesXYZFAIL.com"),
}.ToObservable();
var currentRequestResults = from uri in query
let request = WebRequest.Create(uri)
from result in Observable.FromAsyncPattern(request.BeginGetResponse, ar =>
{
request.EndGetResponse(ar);
return true;
})()
.Catch<bool, WebException>(_ => Observable.Return(false))
select new
{
Uri = uri,
Result = result
};
currentRequestResults.Where(newResult => newResult.Result)
.Subscribe((newResult) =>
{
Console.WriteLine(newResult.Uri);
proxyQueue.Enqueue(newResult.Uri);
});
Console.ReadLine();
}
}
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Pokud je IsAvailable skutečně IO operace, tak je možné použít RegisterWaitForSingleObject. Interně se používají IO completion porty a a neblokuje se vlákno v aplikaci.
http://msdn.microsoft.com/en-us/library/w9f75h7a.aspx
Případně prosím napiš, co metoda IsAvailable skutečně dělá. Myslím, že budeš moci použít i await, když metoda IsAvailable vrátí Task, ale IO completion porty by byly lepší.