开发者

Implement an Async timeout in Silverlight?

开发者 https://www.devze.com 2023-03-13 23:13 出处:网络
In Silverlight, say we start an async request: var request = WebRequest.Create(uri); and then wait for a response via a delegate

In Silverlight, say we start an async request:

var request = WebRequest.Create(uri);

and then wait for a response via a delegate

request.BeginGetResponse(getResponseResult => ...

How do we timeout this waitee, to deliver a time-out error signal to the delegate instead? Use a timer? (note: timeout options available in the .net framework are missing in the Silverlight version).

To handle two messages arriving at the same time, we could introduce a Guid filter, and then ignore a 2nd message when/if it was delivered to the delegate. Or in reverse (better), we register an expiring Guid so that the delegate can discard a second message -- at least some garbage collection is possible then (although the memory footprint of a delegate has got to be on the small side).

What follows are some notes I've made. I haven't reached a conclusion yet. Any help much appreciated.


My line of enquiry was going to be to implement a timer, notify the callback, and then somehow cancel waiting delegate in request.BeginGetResponse(...).

Note:

  • While the .Net Frame Work 4 implements a timeout on the WebRequest class, the Silverlight version does not.

  • "System.Threading.Task.Wait Method (TimeSpan)" is not available either

QUESTION 1: is there a better way to implement a timeout error to the same delegate target?

QUESTION 2: To cancel the waiting delegate, is it sufficient to use "request.BeginGetResponse(null)"?

QUESTION 3: will a non executed delegate (e.g. getResponseResult => ...) cause a small memory leak? Is this just a minor concern?


Information on creating a timer:

  • System.Windows.Threading.DispatcherTimer

  • http://blog.bodurov.com/How-to-Create-setTimeout-Function-in-Silverlight

Some References:

  • http://msdn.microsoft.com/en-us/library/system.servicemodel.channels.binding.opentimeout(v=VS.95).aspx (seems to be WCF related)

  • http://msdn.microsoft.com/en-us/library/system.servicemodel.channels.binding.receivetimeout(v=VS.95).aspx (seems to be WCF related)

  • http://blog.bodurov.com/How-to-Create-setTimeo开发者_如何学Gout-Function-in-Silverlight (could be useful)

  • Silverlight, dealing with Async calls (no new information)

  • http://petesbloggerama.blogspot.com/2008/07/omg-silverlight-asynchronous-is-evil.html (I want to deal with Async properly... I've got some good techniques, but I want to ensure that I'm using them cleanly. Probably I need to look into delegates more closely).

  • http://msdn.microsoft.com/en-us/library/dd460717.aspx#CommunityContent

  • http://csharperimage.jeremylikness.com/2010/03/sequential-asynchronous-workflows-in.html (some good information. Still haven't found out what to do about infinitely waiting delegates).


If you feel like giving reactive extensions a try you will get time out support for cheap. With Rx it will look something like this (not exact code):

var obsrv = Observable.FromAsyncPattern<...>(Begin..., End...);
obsrv(...).Timeout(new TimeSpan(0,0,5)).Subscribe(() => DoThings());
0

精彩评论

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

关注公众号