Click here to Skip to main content
13,146,014 members (46,579 online)
Click here to Skip to main content
Add your own
alternative version

Stats

14.8K views
5 bookmarked
Posted 21 Sep 2011

Asynchronous Image Download with Abort-on-Timeout for WP7

, 31 Mar 2017
Rate this:
Please Sign up or sign in to vote.
AutoResetEvent can be used in place of AsyncWaitHandle and provides a thread-blocking WaitOne signaled timeout function, which can be put on a background thread in order to not block the main UI thread.

While working on my House Ad Framework (part of my Advertising Network Manager XNA Component that starts its private-beta this week), I needed a way to download PNG files from the Internet asynchronously and specify a timeout, where the asynchronous request would abort if the wait period expired before receiving the image’s file stream. I searched the Web and found BeginGetResponse and the sample code provided on that MSDN page. I modified it to load image files instead of HTML, but still couldn’t use it because IASyncResult.AsyncWaitHandle throws an “unsupported” exception at run-time on Windows Phone. Other solutions that I found were similarly unsupported on WP7.

Fortunately, I soon found AutoResetEvent, which can be used in place of AsyncWaitHandle and provides a thread-blocking WaitOne signaled timeout function, which can be put on a background thread in order to not block the main UI thread. I posted my code for doing the “asynchronous image download with abort on timeout” on the AppHub forum yesterday, and you’re welcome to it! I tested it briefly and it works, but if you find any bugs, please let me know.

Note that the code I posted uses a few simple things that won’t compile until you make some minor changes:

public static class DebugHelper {
    [Conditional("DEBUG")]
    public static void WriteLineMT(string text) {
        Debug.WriteLine(DateTime.Now + " [" + 
          Thread.CurrentThread.ManagedThreadId + "]: " + text);
    }
}
  1. My “HouseAdFrame” class “frame” object – I didn’t provide the class, but the only things used from it are the “Image” (an XNA Texture2D object) and “ImagePath” (a string containing the image file URL) members. You should be able to easily replace the AsyncRequestStateframe” member with those two things.
  2. As noted above, this code loads an XNA Texture2D object from the response stream. If you are using Silverlight, you will need to change this to a Silverlight-compatible type.
  3. My “removeFrameList” code in the exception handlers (catch-blocks). When the image-load fails, it means that the frame is going to be bad, so I put it on a list of frames to be removed on the next Update cycle. You will want to replace this exception handler code with your own error processing code.
  4. My use of the DebugHelper.WriteLineMT function, which is provided below for your reference:

Of course, as it usually goes, you spend hours figuring something out only to find that someone may have a better solution and it was freely available to you all along. While I haven’t tried it yet, you may want to check out Wintellect’s “Power Threading” library download by the amazing Jeffrey Richter. Having read many of his books over the years, it’s quite likely that while my solution may fill your immediate need, his solution will cover many usage scenarios that mine doesn’t.

Now go forth and download asynchronously to your heart’s content!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Dan Colasanti
President ImproviSoft LLC
United States United States
Dan is the Founder and President of ImproviSoft LLC (mobile software) and AdStreamer, Inc. (mobile advertising) - both Microsoft BizSpark Plus Startups.

Dan holds a B.S. in Computer Science from Clarkson University and M.S. degrees in Computer Science and Computer Engineering from Syracuse University. He is an ASQ Certified Software Quality Engineer (CSQE) and was a 2012 Microsoft XNA/DirectX MVP.

Prior experience includes Software Engineering, Project Management, and Functional Management in the Aerospace & Defense, Medical Devices, Automotive Engineering, and e-Commerce industries.

Dan's dev-blog is The ImproviSoft Blog.

You may also be interested in...

Pro
Pro

Comments and Discussions

 
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170915.1 | Last Updated 1 Apr 2017
Article Copyright 2011 by Dan Colasanti
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid