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

  • SEARCH
  • Home
  • 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 3976490
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T04:48:39+00:00 2026-05-20T04:48:39+00:00

I would like call HttpWebRequest on another thread as UI, because I must make

  • 0

I would like call HttpWebRequest on another thread as UI, because I must make 200 request or server and downloaded image.

My scenation is that I make a request on server, create image and return image. This I make in another thread. I use Task class, but it call automaticaly Dispose method on all object created in task scope. So I return null object from this method.

    public BitmapImage CreateAvatar(Uri imageUri, int sex)
    {
        if (imageUri == null)
            return CreateDefaultAvatar(sex);

        BitmapImage image = null;

        new Task(() =>
        {
            var request = WebRequest.Create(imageUri);
            var response = request.GetResponse();
            using (var stream = response.GetResponseStream())
            {
                Byte[] buffer = new Byte[response.ContentLength];
                int offset = 0, actuallyRead = 0;
                do
                {
                    actuallyRead = stream.Read(buffer, offset, buffer.Length - offset);
                    offset += actuallyRead;
                } while (actuallyRead > 0);


                    image = new BitmapImage
                    {
                        CreateOptions = BitmapCreateOptions.None,
                        CacheOption = BitmapCacheOption.OnLoad
                    };
                    image.BeginInit();

                    image.StreamSource = new MemoryStream(buffer);

                    image.EndInit();

                    image.Freeze();

            }
        }).Start();

        return image;
    }

How avoid it? Thank

  • 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-20T04:48:40+00:00Added an answer on May 20, 2026 at 4:48 am

    You don’t wait for the Task.

    public BitmapImage CreateAvatar(Uri imageUri, int sex)
    {
       BitmapImage image = null;
    
       new Task(() =>
        {
            ....
        }).Start();
    
    
       return image;  // But the task is still running ...
    } 
    

    To solve it, you would have to Wait() on the Task but then it would be better not to use a task at all here.

    A real solution will have to be worked out in the wider program.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a controller action which I would like to call another controller action.
I would like the server to call a URL (an ashx page) programmatically and
I would like to call a Ruby script when a user uploads an image
I would like to call python script files from my c++ program. I am
I would like to call Perl script files from my c++ program. I am
I would like to call my unmanaged C++ libraries from my C# code. What
I would like to call C# service with using PHP, anyone know how to
I would like to call FindLast on a collection which implements IEnumerable , but
I would like to call a windows program within my code with parameters determined
From my bash shell I would like to call a program n times with

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.