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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T04:23:24+00:00 2026-06-15T04:23:24+00:00

I have an Asynchronous callback function for a silverlight/WP7 application that looks like the

  • 0

I have an Asynchronous callback function for a silverlight/WP7 application that looks like the following.

public static my_function()
{
     PostClient conn = new PostClient(POST);
            conn.DownloadStringCompleted += (object sender2, DownloadStringCompletedEventArgs z) =>
            {
                if (z.Error == null)
                {
                    //Process result
                    string data = z.Result;
                    //MessageBox.Show(z.Result);  

                    //Convert Login value to true false
                    try
                    { ... do stuff here

                    }
}

I want to be able to use the callback functions to return data values in pre-existing methods; i.e.

public List<Usernames> GetUsernames()
{
       List<Usernames> user_list = my_funtion();
       return user_list;
}

At the moment i am having the callback functions update static variables which triggers an event and is a nuisance working with alot of data and keeping track of it all, especially when every data variable needs its own function and static variable.

Whats the best method of doing 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-06-15T04:23:25+00:00Added an answer on June 15, 2026 at 4:23 am

    Tasks to the rescue!

    public static Task<List<Usernames>> my_function()
    {
        var tcs = new TaskCompletionSource<List<Usernames>>(); //pay attention to this line
    
        PostClient conn = new PostClient(POST);
        conn.DownloadStringCompleted += (object sender2, DownloadStringCompletedEventArgs z) =>
        {
            if (z.Error == null)
            {
                //Process result
                string data = z.Result;
                //MessageBox.Show(z.Result);  
    
                //Convert Login value to true false
                try
                {
                    ///... do stuff here
    
                    tcs.SetResult(null); //pay attention to this line
                }
                finally
                {
                }
            }
            else
            {
                 //pay attention to this line
                tcs.SetException(new Exception());//todo put in real exception
            }
        };
    
        return tcs.Task; //pay attention to this line
    }
    

    I stuck in some //pay attention to this line comments to emphasize the few lines of code I added from your existing code.

    If you’re using C# 5.0 you can await on the result when calling the function. If you’re in a context in which it’s acceptable to perform a blocking wait you can just use Result on the returned task right away (be careful when doing that in certain environments) or you could use continuations of the returned task to process results (which is what await will do behind the scenes).

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

Sidebar

Related Questions

I have a callback function that I'd like to execute after 2 separate $.getScript
I have lot of code in my Tornado app which looks like this: @tornado.web.asynchronous
I have a Silverlight 2 application that is consuming a WCF service. As such,
I have an Android class which has a callback from an asynchronous HTTP process,
I have a rails app that has asynchronous processing, and I'm having trouble getting
I have a contact us form that uses Ajax (i.e. relies on asynchronous requests).
Let's say you have a Javascript function that calls a web service method. So
I understand that Javascript doesn't have multiple threads, but I'd like to know if
I have a large script that is very synchronous. 1) it runs a function
Current Asynchronous JavaScript calls require us to use a Callback function. This can lead

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.