Question

Dans gestionnaire Async je crée un IObservable de WebRequest qui retourne une chaîne de redirection.

Je vous abonnant à cette observable et appelant AsyncResult.CompleteCall () mais je suis obligé d'utiliser Thread.Sleep (100) afin d'obtenir exécuté. Et cela ne fonctionne pas à chaque fois. Je suis sûr que ce n'est pas correct. Pourriez-vous s'il vous plaît briller un peu de lumière. Merci!

    public IAsyncResult BeginProcessRequest(HttpContext context, AsyncCallback cb, object state)
    {
        _context = context;
        _ar = new AsyncResult(cb, state);

        _tweet = context.Request["tweet"];
        string url = context.Request["url"];

        if(String.IsNullOrEmpty(_tweet) || String.IsNullOrEmpty(url))
        {
            DisplayError("<h2>Tweet or url cannot be empty</h2>");
            return _ar;
        }

        _oAuth = new oAuthTwitterRx();
        using (_oAuth.AuthorizationLinkGet().Subscribe(p =>
        {
            _context.Response.Redirect(p);
            _ar.CompleteCall();
        },
                exception => DisplayError("<h2>Unable to connect to twitter, please try again</h2>")
                ))


        return _ar;
    }

public class AsyncResult : IAsyncResult
{
    private AsyncCallback _cb;
    private object _state;
    private ManualResetEvent _event;
    private bool _completed = false;
    private object _lock = new object();

    public AsyncResult(AsyncCallback cb, object state)
    {
        _cb = cb;
        _state = state;
    }

    public Object AsyncState
    {
        get { return _state; }
    }

    public bool CompletedSynchronously
    {
        get { return false; }
    }

    public bool IsCompleted
    {
        get { return _completed; }
    }

    public WaitHandle AsyncWaitHandle
    {
        get
        {
            lock (_lock)
            {
                if (_event == null)
                    _event = new ManualResetEvent(IsCompleted);
                return _event;
            }
        }
    }

    public void CompleteCall()
    {
        lock (_lock)
        {
            _completed = true;
            if (_event != null)
                _event.Set();
        }

        if (_cb != null)
            _cb(this);
    }
}
Était-ce utile?

La solution

Mieux vaut tard que jamais; Je pense que l'article suivant explique exactement ce que vous essayez d'atteindre: http://blogs.msdn.com/b/jeffva/archive/2010/09/15/rx-on-the-server -part-5-de-n-observable-asp-net.aspx

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top