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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T14:57:22+00:00 2026-06-15T14:57:22+00:00

I’m writing a wrapper that allows me to invoke methods on an arbitrary SynchronizationContext

  • 0

I’m writing a wrapper that allows me to invoke methods on an arbitrary SynchronizationContext, for example to allow me to queue network related callbacks (e.g. Socket.BeginReceive) to some arbitrary dispatcher/handler, such as a UI thread, or my own implementation designed for serial execution. (to avoid the need to synchronize data structures – locks etc..).

Basically something like this:

..
public void BeginInvoke(MethodCall methodCall)
{
    this.synchronizationContext.Post(this.SynchronizationContextCallback, methodCall);
}

private void SynchronizationContextCallback(Object methodCall)
{
    (methodCall as MethodCall).Invoke();
}
..

It all seemed to work fine with aWindowsFormsSynchronizationContext but when an exception was thrown (A System.Reflection.TargetInvocationException) I realized that that the SendOrPostCallback delegate is being dynamically invoked(?!) here’s the relevant code (Microsoft Reference) from the System.Windows.Forms.Control.ThreadMethodEntry class:

private static void InvokeMarshaledCallbackDo(ThreadMethodEntry tme)
{
    // We short-circuit a couple of common cases for speed.
    //
    if (tme.method is EventHandler)
    {
        if (tme.args == null || tme.args.Length < 1)
        {
            ((EventHandler)tme.method)(tme.caller, EventArgs.Empty);
        }
        else if (tme.args.Length < 2)
        {
            ((EventHandler)tme.method)(tme.args[0], EventArgs.Empty);
        }
        else
        {
            ((EventHandler)tme.method)(tme.args[0], (EventArgs)tme.args[1]);
        }
    }
    else if (tme.method is MethodInvoker)
    {
        ((MethodInvoker)tme.method)();
    }
    else if (tme.method is WaitCallback)
    {
        Debug.Assert(tme.args.Length == 1,
                        "Arguments are wrong for WaitCallback");
        ((WaitCallback)tme.method)(tme.args[0]);
    }
    else
    {
        tme.retVal = tme.method.DynamicInvoke(tme.args);
    }
} 

There appears to be no support for SendOrPostCallback delegate but it’s interesting to note its signature is completely identical to a WaitCallback! or more generally, an Action<Object>. This leaves me in question, am I doing something wrong here? or is this by design? (both on the language and framework level I mean..). Obviously.. having all the dispatched method calls dynamically invoked would be significantly slower and more difficult to debug? (to the point where I might not even find this a usable solution?). What am I missing here?

  • 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-15T14:57:23+00:00Added an answer on June 15, 2026 at 2:57 pm

    This is inevitable, it can marshal any delegate type. The only way to do that is to use DynamicInvoke(). They did make the effort to pick off some common delegate types you’d use in Winforms programming but that can never be an exhaustive list without losing the benefit of doing so. There is little point in worrying about the cost, this always involves a thread context switch and the latency of the Winforms message loop responding to the PostMessage(). The cost of DynamicInvoke() is small potatoes compared to that.

    And yes, you do have to do something to avoid getting slapped by the outer-most exception, the TargetInvocationException isn’t useful to anybody. The Winforms Control.Invoke() method does that by re-throwing only the inner-most InnerException. That’s not ideal either but better than TIE.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a

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.