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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T05:11:32+00:00 2026-06-03T05:11:32+00:00

The main problem is, that raising an event from one thread can invoke delegates

  • 0

The main problem is, that raising an event from one thread can invoke delegates which should only invoked in a certain thread context. After doing some research on this problem i thought, maybe its possible to pass some kind of synchronization context along with each subscription to an event:

SomeClass.SmartSyncEvent += (myDelegate, someReferenceToAThread);

and then raise the event and it somehow does:

foreach(subscriber)
{
    someReferenceToAThread.Invoke(myDelegate);
}

This is super pseudo code, but maybe someone has already done things like this, or knows of any .NET classes which can set up such a pattern.
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-06-03T05:11:33+00:00Added an answer on June 3, 2026 at 5:11 am

    The best way to do this is pass SomeClass a synchronization context via ISynchronizeInvoke.

    public class SomeClass
    {
        public event EventHandler SmartSyncEvent;
    
        public ISynchronizeInvoke SynchronizingObject { get; set; }
    
        public void RaiseSmartSyncEvent()
        {
            if (SynchronizingObject != null)
            {
                SynchronizingObject.Invoke(
                  (Action)(()=>
                  {
                      SmartSyncEvent();
                  }), null);
            }
            else
            {
                SmartSyncEvent();
            }
        }
    }
    

    This pattern is similar to the way System.Timers.Timer is implemented. The problem with it is that every subscriber will be marshaled onto the same synchronizing object. It does not appear that this is what you want though.

    Fortunately delegates store the class instance upon which the method should be invoke via the Target property. We can exploit this by extracting it before we invoke the delegate and using it as the synchronizing object assuming of course that it is a ISynchronizeInvoke itself. We could actually enforce that by using custom add and remove event accessors.

    public class SomeClass
    {
        private EventHandler _SmartSyncEvent;
    
        public event EventHandler SmartSyncEvent
        {
            add
            {
                if (!(value.Target is ISynchronizeInvoke))
                {
                    throw new ArgumentException();
                }
                _SmartSyncEvent = (EventHandler)Delegate.Combine(_SmartSyncEvent, value);
            }
            remove
            {
                _SmartSyncEvent = (EventHandler)Delegate.Remove(_SmartSyncEvent, value);
            }
        }
    
        public void RaiseMyEvent()
        {
            foreach (EventHandler handler in _SmartSyncEvent.GetInvocationList())
            {
                var capture = handler;
                var synchronizingObject = (ISynchronizeInvoke)handler.Target;
                synchronizingObject.Invoke(
                    (Action)(() =>
                    {
                        capture(this, new EventArgs());
                    }), null);
            }
        }
    }
    

    This is a lot better in that each subscriber can be marshaled independently of the others. The problem with it is that the handlers must be instance methods that reside in an ISynchronizeInvoke class. Also, Delegate.Target is null for static methods. This is why I enforce that constraint with the custom add accessor.


    You could make it execute the handlers synchronously if Delegate.Target were null or otherwise could not be casted into a useful synchronizing object. There are a lot of variations on this theme.

    In WPF you could code for DispatcherObject instead of ISynchronizeInvoke.

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

Sidebar

Related Questions

Problem description : - Step 1: Take input FILE_NAME from user at main thread.
Main problem: how do i group elements by their Date, only if continuous and
The main problem I'm having is pulling data from tables, but any other general
I'll only try to present the main part of the problem, because the whole
Can't understand what is a problem here: I have got main.cpp file where I
I am using jquery notifications, but the main problem is that it adds the
My main problem is that I need to enable multiple OS processes to communicate
I'm still with ndk-gdb, now trying to solve the main problem that leaded me
I have another problem that I can't solve I have a following code that
My main problem is that I want to create an accordion based website in

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.