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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:38:55+00:00 2026-05-28T06:38:55+00:00

I am using Func<> or Action<>.BeginInvoke to run methods asynchronously using the ThreadPool. Is

  • 0

I am using Func<> or Action<>.BeginInvoke to run methods asynchronously using the ThreadPool.

Is it possible to have the AsyncCallback invoke a function (or an event, technically) on the original thread that spawned the new thread?

I know that in WinForms apps you can use Control / ISynchronizeInvoke / Dispatcher to alert the UI thread when async operations complete — but this isn’t a WinForms app and it does not seem to work.

    class SyncTest : System.ComponentModel.ISynchronizeInvoke
    {
        public void TestMethod() {
            Console.WriteLine(System.Threading.Thread.CurrentThread.ManagedThreadId + " Test Method Fired.");
        }

        public IAsyncResult BeginInvoke(Delegate method, object[] args) {
            throw new NotImplementedException();
        }

        public object EndInvoke(IAsyncResult result) {
            throw new NotImplementedException();
        }

        public object Invoke(Delegate method, object[] args) {
            return method.DynamicInvoke(args); 
        }

        public bool InvokeRequired {
            get { throw new NotImplementedException(); }
        }

        public void Test()
        {
            var sleep = new Action(() => System.Threading.Thread.Sleep(5000));

            System.ComponentModel.ISynchronizeInvoke originalThreadCallback = (System.ComponentModel.ISynchronizeInvoke)this;

            for (int i = 0; i < 5; i++)
            {
                sleep.BeginInvoke(new AsyncCallback(res =>
                {
                    (res.AsyncState as Action).EndInvoke(res);
                    Console.WriteLine("Thread inside callback: " + System.Threading.Thread.CurrentThread.ManagedThreadId); 
                    originalThreadCallback.Invoke(new Action(() => this.TestMethod()), null);
                }), sleep);
            }
        }
    }

The output from the Test() method is as follows:

Main thread = 9

TEST DONE

Thread inside callback: 11

Thread inside callback: 10

10 Test Method Fired.

11 Test Method Fired.

Thread inside callback: 12

12 Test Method Fired.

Thread inside callback: 13

13 Test Method Fired.

Thread inside callback: 14

14 Test Method Fired.

As you can see nothing is invoked on the original thread with ID = 9.

Obviously my implementation of ISynchronizeInvoke isn’t actually doing anything to invoke the Test Method on the original thread, and thats the problem — but I also can’t seem to get an ISynchronizeInvoke instance out of any of my delegates or events (it is always null). What objects in .NET 4 implement the interface correctly?

Thanks.

  • 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-28T06:38:56+00:00Added an answer on May 28, 2026 at 6:38 am

    Well, you’d have to have some sort of loop, just like Windows Forms (etc) do, waiting for work to process.

    You could write that yourself, but it would mean your original thread would have to be kept relatively free, just like you don’t want to block the UI thread in a Windows Forms app. Basically, you’d be rewriting some of the infrastructure that WinForms uses. There’s nothing I’m aware of that just does that out of the box, although the .NET 4 producer/consumer queue classes (e.g. BlockingCollection<T>) would at least help.

    The docs for BlockingCollection<T> give a quick example of how to implement a producer/consumer queue – basically your threadpool threads would call Add on the queue, and your consumer thread would call Take on it (or TryTake).

    There’s also a Channel-9 video with expert Stephen Toub (of the parallel team in MS) about using BlockingCollection.

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

Sidebar

Related Questions

I have a PHP function that takes a variable number of arguments (using func_num_args()
C# has generic function types such as Action<T> or Func<T,U,V,...> With the advent of
What speaks against using the delegates System.Action or System.Func as EventDelegates instead of the
Say I have a templated Action template <class ArgT> struct Action { Action(::boost::function< void(ArgT)
I much prefer using an Action or a Func if I need a quick
I am testing for Exceptions using unittest, for example: self.assertRaises(UnrecognizedAirportError, func, arg1, arg2) and
Using C# for ASP.NET and MOSS development, we often have to embed JavaScript into
I'm using a jQuery .each() call, which uses an anonymous function, from inside a
I have the following code that I have been using to Set properties in
I am adding range of integers (101,105) using Func<> delegate.I suppose to get 101,102,..105

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.