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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:41:14+00:00 2026-05-28T01:41:14+00:00

The included C# unit test and C code file attempts to pass a managed

  • 0

The included C# unit test and C code file attempts to pass a managed callback to unmanaged code. The code actually runs but the count variable never increments. So the test fails.

The fact that it runs at all means that it does load the dll, finds the reference for the DoCallBack() method, and it seems to call the method. But nothing happens. So something is off.

You probably want to know why attempt to do this? And you wonder if there’s a better approach? Well, the end goal is to create a “hack” so as run threads across AppDomains at nearly the same performance as in the same domain.

At the following link you’ll find the faster, so far, technique on cross AppDomain performance. The MS .Net AddIn team offers “FastPath” which improves alot over simple remoting performance. We ran their example on .Net 3.5 and it works very fast after putting their AddIn contracts into the GAC.

http://blogs.msdn.com/b/clraddins/archive/2008/02/22/add-in-performance-what-can-you-expect-as-you-cross-an-isolation-boundary-and-how-to-make-it-better-jesse-kaplan.aspx

Now let’s discuss some of the timing coparisons to see why that’s still not fast enough for our needs. Normal cross domain remoting offers approximately 10,000 calls per second on a method with zero arguments. With the FastPath option that increases to 200,000 calls per second. But comparing that to the C# of calling an interface method with zero arguments (in the same domain), it does over 160,000,000 operations per second on the same machine as the other tests.

So even the FastPath technique is still 1,000 times slower than simple interface method call. But why do we need better performance?

Or performance requirements are to remove all software bottlenecks from a CPU bound application that process billions of tuples of information in mere minutes using multicore and distributed technology.

But a new feature requirement will be the ability to offer an AddIn, or Plugin architure so that components can be loaded or unloaded w/o stopping the rest of the system. The only way to do that effectively on .Net is with separate AppDomains.

Please note that we don’t want to communicate data across AppDomains, they all operate independently in parallel.

But as far as threading, it’s very inefficient to have a separate thread running in each of hundreds of AppDomains. If so they compete for CPU and cause huge loss of performance from context switching.

So again, the plan here is to have a primary or master domain which has a thread pool and takes turns calling into each AppDomain that has work to do, and let it work a while. So that means cooperative multi-threading (to avoid context switching). Therefore, the AppDomains will return to allow the main AppDomain to move on to others.

Unfortunately each AppDomain can’t run very long independently before it runs out of work and needs to return to the master domain to let a different AppDomain do work..so the performance times of 200,000 per second from the FastPath technique will cause a significant slow down in overall performance due to cross AppDomain calls.

In contrast with PInvoke below we have measured the timing with StopWatch to produce over 90,000,000– that’s 90 million– calls per second on the same machine as the other tests. So the hope is that by then reverse P/Invoking into a different AppDomain it will still allow for many millions of operates per second.

90 million per second is much closer to our need for switching threads between AppDomains.

Okay. Now back to this unit test. The purpose of this simply unit test is to first get simple call backs from unmanaged to managed code working….after that, the next step will be to create a separate AppDomain and get a delegate callback to it and pass to the unmanaged code to test the cross domain callback performance.

We know that all this is possible we see discussion and examples on the web…but while the code below seems simple…it’s just not working as expected.

Here’s the unmanaged code built as a DLL w/o the /CLR command line option:

#include <windows.h>

typedef void (CALLBACK *PFN_MYCALLBACK)();
int count = 0;

extern "C" {
    __declspec(dllexport) void __stdcall DoSomeStuff() {
        ++count;
    }
}

extern "C" {
    __declspec(dllexport) void __stdcall DoCallBack(PFN_MYCALLBACK callback) {
        PFN_MYCALLBACK();
    }
}

Here’s the C# unit test code.

using System.Runtime.InteropServices;
using System.Security;
using NUnit.Framework;

namespace TickZoom.Callback
{
    [SuppressUnmanagedCodeSecurity]
    [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
    public delegate void MyCallback();

    [TestFixture]
    class CrossAppDomainTesting
    {
        public int count;
        [Test]
        public void TestCallback()
        {
            NativeMethods.DoCallBack(
                delegate()
                {
                    ++count;
                });
            Assert.AreEqual(1, count);
        }

        [Test]
        public void TestDate()
        {
            NativeMethods.DoSomeStuff();
        }
    }

    public static class NativeMethods
    {
        [SuppressUnmanagedCodeSecurity]
        [DllImport("CrossAppDomain.dll")]
        public static extern void DoSomeStuff();

        [SuppressUnmanagedCodeSecurity]
        [DllImport("CrossAppDomain.dll")]
        public static extern void DoCallBack(MyCallback callback);

    }
}
  • 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-28T01:41:15+00:00Added an answer on May 28, 2026 at 1:41 am

    The first commenter to the post solved the coding problem. I waited for him to post it as an answer to give him credit. If he does, then I will.

    Is that just a typo in your DoCallback function? You have PFN_MYCALLBACK(). I think you want
    it to be callback(). – Jim Mischel

    Also, the result of the timings so that the fastest way possible to call from one AppDomain across to another AppDomain is the following:

    You first call an unmanaged method to send over a delegate to unmanaged code which gets marshaled to a function pointer.

    From that point on you call the unmanaged code w/o any arguments but reuse the function pointer to call into the other AppDomain.

    Our testing shows that this works at 90 million calls per second as compared to 300 million per second of a simply C# call on an interface or a Interlocked.Increment() that is 80 million per second.

    In other words, this is fast enough to happen quite often to transition threads across AppDomain boundaries.

    NOTE: There are a couple things to be careful with. If you keep pointers to AppDomains that you unload and then try to call them, you’ll get an exception about a collected delegate. The reasons for this is that the function pointer that the CLR gives you isn’t a simply function pointer into the code. Instead it’s a pointer to a piece of “thunking” code which first checks that the delegate is still around, and it does a little housekeeping for the transition from unmanaged to managed code.

    Our plan is to assign each AppDomain an integer handle. Then the unmanaged code will get both the “handle” and the function pointer to put into an array.

    When we unload an AppDomain we’ll also inform the unmanaged code to remove the function pointer for that handle. We’ll keep the freed ‘handle’ on a freed list to reuse for the next AppDomain created.

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

Sidebar

Related Questions

Some code I am unit testing needs to load a resource file. It contains
I'm trying to use embedded ActiveMQ broker for unit testing, as explained here: http://activemq.apache.org/how-to-unit-test-jms-code.html
Should unit tests be included in the same repo as the tested code? Or
How can I convert the following code to use boost unit test framework: #include
I have a unit test which depends on some code that uses MEF. When
I'm trying to unit test a piece of code that I've written in Ruby
I'm programming some unit test with the Google test framework. But I want to
I have included <zlib.h> in my iphone application and the source code I was
I need to execute my code after finalization of SysUtils unit. I've placed my
I am running my tests for a rails application using test/unit and capybara. I

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.