开发者

Synchronous handler for OpenReadCompletedEventArgs

My current project uses asynchronous methods to download files thro开发者_Python百科ugh a WebService (due to SL restrictions). I'm trying to write an overriding class that uses the same interface but actually reads files synchronously from the XAP. I've verified that I'm able to access file streams in the XAP as expected.

However, I don't know how to fire my handler and read the file synchronously. It looks like OpenReadCompletedEventArgs.Result is the property that I should be setting my stream to. However, I'm unable to set this property since it is read-only.

How do I go about constructing an object of this type with the stream set? Or is there something else I should be doing to achieve what I want without changing the interfaces.


Can't quite make sense of this but I think what you have is some existing code that consumes OpenReadCompletedEventArgs and you want to re-use this code when the stream retrieved is actually from the Xap not a downloaded resource.

You will not be able to inherit or mutate the OpenReadCompletedEventArgs class because all its constructors are private.

Hence you will need to change your existing code. You are probably only really interested in the Stream so in reality you need only move most of your code to functions that accept a Stream object. Now code using an OpenReadCompleted event would then call these functions passing e.Result and you new code can simply retrieve the stream from Xap and pass it to these same functions.


I wrote a separate event handler delegate and handler args to handle this situation. My event handler args can be instantiated from an OpenReadCompletedEventArgs object. I made sure the same property names were used so that the calling code wouldn't have to change.

In addition, in my event handler function for the asynchronous code, I simply added an anonymous event handler that called my custom event handler class:

public void FunctionUsingHandler(MyCustomHandler handler)
{
    WebClient c;
    ...
    c.OpenReadCompleted += new OpenReadCompletedEventHandler((sender, e) => handler(sender, new MyCustomHandlerArgs(e)));
    ...
}

This way, my handler is only run after the read operation has completed. Synchronous users can just immediately call my handler with the appropriate args.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜