private class RequestState
{
    public HttpWebRequest Resquest { get; set; }
    public HttpWebResponse Response { get; set; }
    public ManualResetEvent ResetEvent { get; set; }
}
public static IEnumerable<string> GetFolders(string uri)
{
    var requestState = new RequestState();
    requestState.Resquest = (HttpWebRequest)WebRequestCreator.ClientHttp.Create(new Uri(uri, UriKind.Absolute));
    requestState.ResetEvent = new ManualResetEvent(false);
    requestState.Resquest.BeginGetResponse(GetFoldersCallback, requestState);
    requestState.ResetEvent.WaitOne();
    return ProcessFolderResponse(requestState.Response);
}
private static void GetFoldersCallback(IAsyncResult result)
{
    var requestState = (RequestState)result.AsyncState;
    requestState.Response = (HttpWebResponse)requestState.Resquest.EndGetResponse(result);
    requestState.ResetEvent.Set();
}The best I could do was to ask for my own callback and wrap that in a call to
Dispatcher.BeginInvoke.public static void GetFolders(Dispatcher dispatcher, string uri, Action<IEnumerable<string>> folderAction)
{
    var request = (HttpWebRequest)WebRequestCreator.ClientHttp.Create(new Uri(uri, UriKind.Absolute));
    request.BeginGetResponse(ar =>
    {
        var response = request.EndGetResponse(ar);
        var folders = ProcessFolderResponse(response);
        dispatcher.BeginInvoke(() => folderAction(folders));
    }, null);
} 
No comments:
Post a Comment