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 6057209
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:27:47+00:00 2026-05-23T08:27:47+00:00

I have this handler. I had an exception occuring in the ‘StartTransfer’ method of

  • 0

I have this handler. I had an exception occuring in the ‘StartTransfer’ method of the inner class (I’ve marked the spot), and for reason I don’t know it went looping this method. Why did it go loops and didn’t just responded in exception message?

public sealed class ImageUploadHandler : IHttpAsyncHandler
{
    public bool IsReusable { get { return false; } }

    public ImageUploadHandler()
    {
    }
    public IAsyncResult BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData)
    {
        string token = context.Request.Form["token"];
        string albumId = context.Request.Form["albumId"];
        string imageDescription = context.Request.Form["description"];
        HttpPostedFile imageFile = context.Request.Files["image"];

        ImageTransferOperation ito = new ImageTransferOperation(cb, context, extraData);
        ito.Start(token, albumId, imageDescription, imageFile);
        return ito;
    }

    public void EndProcessRequest(IAsyncResult result)
    {
    }

    public void ProcessRequest(HttpContext context)
    {
        throw new InvalidOperationException();
    }

    private class ImageTransferOperation : IAsyncResult
    {
        private Object state;
        private bool isCompleted;
        private AsyncCallback cb;
        private HttpContext context;

        public WaitHandle AsyncWaitHandle
        {
            get { return null; }
        }

        public bool CompletedSynchronously
        {
            get { return false; }
        }

        public bool IsCompleted
        {
            get { return isCompleted; }
        }

        public Object AsyncState
        {
            get { return state; }
        }

        public ImageTransferOperation(AsyncCallback cb, HttpContext context, Object state)
        {
            this.cb = cb;
            this.context = context;
            this.state = state;
            this.isCompleted = false;
        }

        public void Start(string token, string albumId, string description, HttpPostedFile file)
        {
            Dictionary<string, Object> dictionary = new Dictionary<string,object>(3);

            dictionary.Add("token", token);
            dictionary.Add("albumId", albumId);
            dictionary.Add("description", description);
            dictionary.Add("file", file);

            ThreadPool.QueueUserWorkItem(new WaitCallback(StartTransfer), dictionary);
        }

        private void StartTransfer(Object state)
        {
            Dictionary<string, Object> dictionary = (Dictionary<string, Object>)state;

            string token = (string)dictionary["token"];
            string albumId = (string)dictionary["albumId"];
            string description = (string)dictionary["description"];
            HttpPostedFile file = (HttpPostedFile)dictionary["file"];

            var media = new Facebook.FacebookMediaObject {
                FileName = file.FileName,
                ContentType = file.ContentType                  
            };

            using (var binaryReader = new BinaryReader(file.InputStream))
            {
                media.SetValue(binaryReader.ReadBytes(Convert.ToInt32(file.InputStream.Length)));
            }

            dictionary.Clear();

            dictionary.Add("message", description);
            dictionary.Add("source", media);

            var client = new Facebook.FacebookClient(token); // <-- Here is where the exception occured

            //var result = client.Post("/" + albumId + "/photos", dictionary);

            context.Response.ContentType = "text/plain";

            context.Response.Write(token + " | " + file.FileName);
            //context.Response.Write(result.ToString());

            isCompleted = true;
            cb(this);
        }
    }
}
  • 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-23T08:27:48+00:00Added an answer on May 23, 2026 at 8:27 am

    I can’t honestly explain whatever it is you’re describing as “looping”, but you are mot handling any exceptions in your async code. You must handle exceptions and propagate them back to the thread who is waiting on the IAsyncResult.

    So, first, you need to have a try/catch/finally around the majority of the body of the StartTransfer method where you capture any potential exception that might occur, store it in a field and, in the finally block, always invoke the callback. Now that you’re actually capturing any potential exception and always calling back, you will next need to test to see if an exception has occurred in the EndProcessRequest and raise it there so that the callback can “observe” it.

    Here’s some revisions to your ImageTransferOperation class:

    private Exception asyncException;
    
    public Exception Exception
    {
        get
        {
            return this.asyncException;
        }
    }
    
    private void StartTransfer(Object state)
    {
        try
        {
            ... rest of implementation here ...
        }
        catch(Exception exception)
        {
            this.asyncException = exception;
        }
        finally
        {
            this.isCompleted = true;
            this.cb(this);
        }
    }
    

    And then to the ImageUploadHandler to propagate the exception back:

    public void EndProcessRequest(IAsyncResult result)
    {
        ImageTransferOperation imageTransferOperation = (ImageTransferOperation)result;
    
        Exception exception = imageTransferOperation.Exception
    
        if(exception != null)
        {
            throw exception;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I somehow have the feeling that modern systems, including runtime libraries, this exception handler
I have this code in a xaml file now in a event handler code
I have this exact issue ASP.net can’t update page from event handler and it's
I have a protocol in Objective-C, something like this: @protocol Handler +(NSString*) getValue; @end
I have had many problems in Django (not all of them solved), but this
In my WPF Window_Loaded event handler I have something like this: System.Threading.Tasks.Task.Factory.StartNew(() => {
We have exception catching code in most of our event handlers etc, this leads
Have you encountered this exception for a stored procedure which does indeed have a
I am curious how others would handle this situation. I have a domain layer
I have turned computer monitor off using this command SendMessage(f.Handle, WM_SYSCOMMAND, (IntPtr)SC_MONITORPOWER, (IntPtr)(turnOff ?

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.