-
-
Save rarous/4059724 to your computer and use it in GitHub Desktop.
| query.Subscribe(x => | |
| { | |
| Task.Factory.StartNew(() => | |
| { | |
| if (IsAvailable(x)) | |
| proxyQueue.Enqueue(x); | |
| }, TaskCreationOptions.LongRunning); | |
| }); |
IsAvailable je dlouho běžící IO operace.
Pokud je IsAvailable skutečně IO operace, tak by např. šlo použít threadpool.registerwaitforsingleobject - interně se používají k dokončení IO completion porty.
http://msdn.microsoft.com/en-us/library/system.threading.threadpool.registerwaitforsingleobject.aspx
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ší.
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ší.
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ší.
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();
}
}
}
Zkoušel jsem ObserveOn TaskPoolScheduler i NewThreadScheduler, ale chová se to dost jinak, než tohle.