Skip to content

Instantly share code, notes, and snippets.

@rarous
Created November 12, 2012 14:30
Show Gist options
  • Select an option

  • Save rarous/4059724 to your computer and use it in GitHub Desktop.

Select an option

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);
});
@rarous
Copy link
Author

rarous commented Nov 12, 2012

Zkoušel jsem ObserveOn TaskPoolScheduler i NewThreadScheduler, ale chová se to dost jinak, než tohle.

@rarous
Copy link
Author

rarous commented Nov 12, 2012

IsAvailable je dlouho běžící IO operace.

@renestein
Copy link

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

@renestein
Copy link

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ší.

@renestein
Copy link

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ší.

@renestein
Copy link

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ší.

@renestein
Copy link

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