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

  • Home
  • SEARCH
  • 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 8949863
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T13:19:03+00:00 2026-06-15T13:19:03+00:00

I want to create a thread with a Dispatcher and then, from another thread,

  • 0

I want to create a thread with a Dispatcher and then, from another thread, use that dispatcher to queue work with different priorities. i.e.

var dispatcher = GetNewThreadDispatcher();
dispatcher.BeginInvoke(longRunningTask1, DispatcherPriority.Normal);
dispatcher.BeginInvoke(longRunningTask2, DispatcherPriority.Background);
dispatcher.BeginInvoke(longRunningTask3, DispatcherPriority.Normal);

In this case, the background thread would perform longRunningTask1, then longRunningTask2, longRunningTask3. I’m having difficulty creating a clean GetNewThreadDispatcher(), any help?

  • 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-15T13:19:04+00:00Added an answer on June 15, 2026 at 1:19 pm

    You can try to create new background thread ( http://msdn.microsoft.com/en-us/library/7a2f3ay4(v=vs.90).aspx) and get Dispatcher for it:

    Dispatcher dispatcher = Dispatcher.FromThread(workerThread);
    

    Some edits:
    You need to execute
    Dispatcher dispatcher=Dispatcher.CurrentDispatcher or Dispatcher.Run inside OnThreadStart. But interesting thing: after execute

    workerThread.Start();
    Dispatcher dispatcher = Dispatcher.FromThread(workerThread);  
    

    dispatcher is null but after execute

    workerThread.Start();
    Console.WriteLine("main thread: Starting worker thread...");
    Dispatcher dispatcher = Dispatcher.FromThread(workerThread); 
    

    dispatcher is filling

    added:

        static void OnThreadStart()
        {
           Dispatcher.Run();
        }
    
    
        private Dispatcher GetNewThreadDispatcher()
        {
            Thread workerThread=null;
            try
            {
               workerThread = new Thread(OnThreadStart);
               workerThread.IsBackground = true;
               workerThread.Start();
               int waitingCiclesCount = 100;
               int cicleIndex = 0;
               int sleepTimeInMiliseconds = 100;
               Dispatcher dispatcher = null;
               while (cicleIndex < waitingCiclesCount)
               {
                   dispatcher = Dispatcher.FromThread(workerThread);
                   if (dispatcher!=null)
                       break;
                   Thread.Sleep(sleepTimeInMiliseconds);
                   cicleIndex = cicleIndex + 1;
    
               }
               if (dispatcher==null)
               {
                   workerThread.Abort();
                   return null;
               }
               Console.WriteLine(String.Format("thread with id={0} started", workerThread.ManagedThreadId));
               return dispatcher;
            }
            catch (Exception)
            {
                if (workerThread!=null)
                    workerThread.Abort();
                return null;
            }
        }
    
        public MainWindow()
        {
            InitializeComponent();
    
            TestWorker worker=new TestWorker();
            Dispatcher dispatcher1 = GetNewThreadDispatcher();
            if(dispatcher1!=null)
                dispatcher1.BeginInvoke(new TestDelegate(worker.DoWork1), DispatcherPriority.Normal);
            else
            {
                MessageBox.Show("Cant create dispatcher1");
            }
            Dispatcher dispatcher2 = GetNewThreadDispatcher();
            if (dispatcher2!=null)
                dispatcher2.BeginInvoke(new TestDelegate(worker.DoWork2), DispatcherPriority.Normal);
            else
            {
                MessageBox.Show("Cant create dispatcher2");
            }
        }   
    

    this code working in my test wpf app but i am not expert in multithreading. May be someone will correct me or add some info to this reply.

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

Sidebar

Related Questions

I have multi-thread application that I want to create a thread with different user
I want to create a background thread that's owned by an object. When that
I want to create a separate thread that runs its own window. Frankly, the
I want to create a worker thread that isn't the main thread so I
I want to create a thread for some db writes that should not block
I want to create a thread in C so that the thread automatically call
I want to create a thread-safe collection that can be modified while being enumerated.
I want to create a thread and then pass parameters to it. But I
I want to create a Thread safe JSP page. It is possible in Servlet
I want to create dynamic number of thread which is depends on the database

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.