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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:06:47+00:00 2026-05-27T17:06:47+00:00

Learning C#, WPF. Currently trying to get to grips with Dispatchers, specifically between threads

  • 0

Learning C#, WPF. Currently trying to get to grips with Dispatchers, specifically between threads and other classes.

I got my dispatcher to work when in the ‘MainWindow’ class, but when I separate it out as below into another class I get the compile error message “The type ‘System.Windows.Threading.Dispatcher’ has no constructors defined”

Clearly a basic learning error, but I can’t figure it out. Code below, a push in the right direction please 🙂

namespace threading_dispatcher_learn
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {

        public delegate void MyDel();

        public MainWindow()
        {
            InitializeComponent();

            label1.Content = "Label1";

            this.label2.Content = this.Dispatcher.Thread.ManagedThreadId;


            Myupdator updateclass = new Myupdator();


            Thread MyNewThread;


            MyNewThread = new Thread(updateclass.UpdateLabel);
            MyNewThread.Start();


        }



        private void button1_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("Clicked");
        }

    }

    public class Myupdator
    {

        public void UpdateLabel()
        {
            MainWindow.MyDel del1 = delegate()
            {

                try
                {
                   // label1.Content = "Update 2";
                    //Thread.Sleep(3000);
                    MessageBox.Show("Message 2");
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            };

           /* Now removed
             Dispatcher myDispather = new Dispatcher();
            myDispather.BeginInvoke(DispatcherPriority.Normal, del1);
*/
        //now added
        Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Normal, del1);
        Dispatcher.CurrentDispatcher.InvokeShutdown();

        }
    }

}

UPDATE: Code modified above, is this the correct way? Am I understanding you correctly? Although nothing in ‘del1’ is getting called.

  • 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-27T17:06:47+00:00Added an answer on May 27, 2026 at 5:06 pm

    You are not supposed to create a Dispatcher instane using a constructor. To access the Dispatcher for the current thread (or create a new one if one does not already exist) simply get the value of Dispatcher.CurrentDispatcher. Do not forget to shut down the dispatcher (e.g. with Dispatcher.InvokeShutdown) when it’s no longer needed.

    However, why are you doing this? In the overwhelming majority of cases, creating a Dispatcher is the wrong thing to do. Dispatcher models a Windows message loop, so it’s only meaningful on UI threads.

    Update: It seems that you are simply trying to modify the contents of the label and show a dialog box. You must not create a new Dispatcher for this; instead, use the Dispatcher already created on your UI thread. You need to pass to your new thread the value of Dispatcher.CurrentDispatcher as taken from your main thread, or alternatively you need to pass a reference to any UI control (e.g. a label, or the window itself).

    For example:

    var mainWindow = ... ; // you need to pass this to your new thread somehow
    mainWindow.Dispatcher.BeginInvoke(DispatcherPriority.Normal, del1);
    

    Let me repeat the rule of thumb once again: unless you have extensive experience in multithreaded applications, creating a new Dispatcher is always the wrong thing to do.

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

Sidebar

Related Questions

While learning c# and wpf, i am trying to get grasp of data binding.
I'm learning this wpf stuff and trying to get my head around validation of
I'm in the process of learning WPF and am currently looking into application commands
I am currently learning to create custom controls in WPF. I successfully created a
I'm learning WPF and have been trying to create a toolstrip. Is there a
I am learning WPF and am trying to create my first UserControl. My UserControl
I'm currently learning WPF. I really am enjoying it so far. I love how
I'm learning WPF and MVVM and trying to stress good design. I have lots
I am learning wpf. I am trying to utilize tdd practices with wpf. I
I'm learning WPF and the MVVM Pattern and I'm trying to build a calendar-like

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.