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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T07:30:19+00:00 2026-06-11T07:30:19+00:00

I have worker class with public event EventHandler<EventArgs> DataModified; which can be raised from

  • 0

I have worker class with

public event EventHandler<EventArgs> DataModified;

which can be raised from other than UI thread (it is network client which fetches updates from server).
I have ModelView with

ObservableCollection<DataModel> DataItems;

to which View binds. My modelview must subscribe to ModifiedEvent, so it can reflect changes in DataItems. How do I update DataItems from callback event? I do not have access to UI Dispatcher from my model view (because view should not be aware of modelview). What is proper .NET 4.5 way for handling this?

  • 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-11T07:30:21+00:00Added an answer on June 11, 2026 at 7:30 am

    You can create a service wich holds a CoreDispatcher and inject into your view model (or make it static)

    public static class SmartDispatcher
    {
        private static CoreDispatcher _instance;
        private static void RequireInstance()
        {
            try
            {
                _instance = Window.Current.CoreWindow.Dispatcher;
    
            }
            catch (Exception e)
            {
                throw new InvalidOperationException("The first time SmartDispatcher is used must be from a user interface thread. Consider having the application call Initialize, with or without an instance.", e);
            }
    
            if (_instance == null)
            {
                throw new InvalidOperationException("Unable to find a suitable Dispatcher instance.");
            }
        }
    
        public static void Initialize(CoreDispatcher dispatcher)  
        {
            if (dispatcher == null)
            {
                throw new ArgumentNullException("dispatcher");
            }
    
            _instance = dispatcher;
        }
    
        public static bool CheckAccess()
        {
            if (_instance == null)
            {
                RequireInstance();
            }
            return _instance.HasThreadAccess;
        }
    
        public static void BeginInvoke(Action a)
        {
            if (_instance == null)
            {
                RequireInstance();
            }
    
            // If the current thread is the user interface thread, skip the
            // dispatcher and directly invoke the Action.
            if (CheckAccess())
            {
                a();
            }
            else
            {
                _instance.RunAsync(CoreDispatcherPriority.Normal, () => { a(); });
            }
        }
    }
    

    You should initialize SmartDispatcher in App.xaml.cs:

     var rootFrame = new Frame();
     SmartDispatcher.Initialize(rootFrame.Dispatcher);
    
     Window.Current.Content = rootFrame;
     Window.Current.Activate();
    

    use it in this way:

    SmartDispatcher.BeginInvoke(() => _collection.Add(item));
    

    This class is based on Jeff Wilcox’s analog for windows phone 7.

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

Sidebar

Related Questions

Example in VB I have a worker class Public Class worker Public Event EventNumber1(ByVal
I have a button click event that will fire a swing worker thread which
I have a layered worker class that I'm trying to get progress reports from.
I have a worker thread in a class that is owned by a ChildView.
I have a background worker which calls a function within a separate class. This
I have a worker thread spawned from a GUI (for GUI performance), how do
I have BasePage.cs class which is being used by other .cs files instead of
lets say i have a background worker in a class that perform db query
I have a class that spawns an arbitrary number of worker object that compute
I have the following manager<->worker situation: class Manager { private: pthread_attr_t workerSettings; pthread_t worker;

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.