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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:09:03+00:00 2026-05-23T14:09:03+00:00

.NET 4.0 has the TPL which contains the nice Task class to encapsulate aynchronous

  • 0

.NET 4.0 has the TPL which contains the nice Task class to encapsulate aynchronous programming models. I’m working on an app that must be .NET 2.0, but I want to avoid rewriting Task. Any suggestions?

  • 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-23T14:09:04+00:00Added an answer on May 23, 2026 at 2:09 pm

    I know you said you dont want to rewrite Task, but you can actually create something fairly simple using closures, which behaves somewhat like a Task object. This is what I use:

        public delegate R AsyncTask<R>();
    
        public static AsyncTask<R> BeginTask<R>(AsyncTask<R> function)
        {
            R retv = default(R);
            bool completed = false;
    
            object sync = new object();
    
            IAsyncResult asyncResult = function.BeginInvoke(
                    iAsyncResult =>
                    {
                        lock (sync)
                        {
                            completed = true;
                            retv = function.EndInvoke(iAsyncResult);
                            Monitor.Pulse(sync); 
                        }
                    }, null);
    
            return delegate
            {
                lock (sync)
                {
                    if (!completed)               
                    {
                        Monitor.Wait(sync); 
                    }
                    return retv;
                }
            };
        }
    

    Its a function that calls BeginInvoke() on the delegate you pass in, and returns a function that when called blocks and waits for the result of the function passed in. You’d have to create overloads of this function for different method signatures, of course.

    One way to go, you can tweak this to your needs, and add other behaviors too like Continuations, etc. The key is to use closures and anonymous delegates. Should work in .NET 2.0.

    EDIT – Here is how you would use it:

        public static string HelloWorld()
        {
            return "Hello World!"; 
        }
    
        static void Main(string[] args)
        {
            var task = BeginTask(HelloWorld); // non-blocking call
    
            string result = task(); // block and wait
    
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have an asp.net mvc app that has a form that uploads a file.
ADO.NET has the notorious DataRow class which you cannot instantiate using new. This is
My gridview in asp.net has a column that shows currency in Euro format which
I'm sure that almost everyone programming in .net has ran into similar issues with
My ASP.NET app has some server-side methods whose results I'd like to inject into
I am using ASP.NET and on ASP.NET page has validate attribute which checks for
I have a website developed in ASP.NET and VB.NET which has a place for
I have a windows 2003 server (WEb edition) which has .net 3.5 sp1 installed
I'm developing an intranet webapp in asp.net that has a requirement for storing user
Like you know, .NET Remoting has some limits, one of them is that server

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.