Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7537435
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T06:51:05+00:00 2026-05-30T06:51:05+00:00

Does anyone know great idea How to return result to UI thread ? I

  • 0

Does anyone know great idea How to return result to UI thread ?
I wrote this code, but It will be compile error because it can’t return “img” in async.

public byte[] DownloadAsync2(Uri address)
{
    byte[] img;
    byte[] buffer = new byte[4096];

    var wc = new WebClient();

    wc.OpenReadCompleted += ((sender, e) =>
    {
        using (MemoryStream memoryStream = new MemoryStream())
        {
            int count = 0;
            do
            {
                count = e.Result.Read(buffer, 0, buffer.Length);
                memoryStream.Write(buffer, 0, count);
            } while (count != 0);

            Deployment.Current.Dispatcher.BeginInvoke(() =>
                {
                    if (e.Error == null) img = memoryStream.ToArray();
                });
        }
    }
    );
    wc.OpenReadAsync(address);

    return img; //error : Use of unassigned local variable 'img'
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T06:51:07+00:00Added an answer on May 30, 2026 at 6:51 am

    Change your method to:

    public void DownloadAsync2(Uri address, Action<byte[]> callback, Action<Exception> exception)
    {
        var wc = new WebClient();
    
        wc.OpenReadCompleted += ((sender, e) =>
        {
            using (MemoryStream memoryStream = new MemoryStream())
            {
                int count = 0;
                do
                {
                    count = e.Result.Read(buffer, 0, buffer.Length);
                    memoryStream.Write(buffer, 0, count);
                } while (count != 0);
    
                Deployment.Current.Dispatcher.BeginInvoke(() =>
                {
                    if (e.Error == null) callback(memoryStream.ToArray());
                    else exception(e.Error);
                });
            }
        }
        );
        wc.OpenReadAsync(address);
    }
    

    Usage:

    DownloadAsync2(SomeUri, (img) =>
    {
        // this line will be executed when image is downloaded, 
        // img - returned byte array
    },
    (exception) =>
    {
        // handle exception here
    });
    

    Or (old-style code without lambda expressions):

    DownloadAsync2(SomeUri, LoadCompleted, LoadFailed);
    
    // And define two methods for handling completed and failed events
    
    private void LoadCompleted(byte[] img)
    {
        // this line will be executed when image is downloaded, 
        // img - returned byte array
    }
    
    private void LoadFailed(Exception exception)
    {
        // handle exception here
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know this site is a great source for code help and snippets but
Does anyone know of a great jquery scrollable div (scrolls with window.scroll) that will
This is a bit of a crazy question, but does anyone out there know
Does anyone know of a good Command Prompt replacement? I've tried bash/Cygwin, but that
Does anyone know a good Java lib that will hook into SVN so I
Does anyone know how I can, in platform-independent C++ code prevent an object from
I'd like to do bellow step by using jquery. Does anyone know great solution?
Does anyone know a great jquery horizontal newsticker? Like: http://www.gcmingati.net/wordpress/wp-content/lab/jquery/newsticker/jq-liscroll/scrollanimate.html But without the white
I'm not too great with javascript. Does anyone know how I can increase the
Does anyone know of a walk-through or any examples of any code to setup

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.