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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T21:36:25+00:00 2026-06-01T21:36:25+00:00

I am using a visual control in my project that is from a library

  • 0

I am using a visual control in my project that is from a library that I do not have the source to.
It takes too long to update (200ms, roughly) for good UI responsiveness with three of these controls on-screen at once. (I might need to update all three at once, which leaves my UI stuck for ~600ms while they are all thinking).

I have read some posts about TaskSchedulers, and am beginning to investigate the Parallel task features as a way of running each of these controls in their own thread. The platform will be multi-core, so I want to take advantage of simultaineous processing.

The problem is that I don’t even know what I don’t know about how to go about this, though..

Is there a suitable design pattern for running a control in a separate thread from the main UI thread in WPF?

Specifically: it is a third party map control, that when given a new location or zoom level takes far too long to redraw (~200ms). With perhaps three of these updating at a maximum of 4Hz – obviously they won’t keep up..
I have encapsulated the WPF control in a usercontrol, and need to run each instance in it’s own thread, while still capturing user input (mouse clicks, for example).

UPDATE: while I am feeling around for a solution, I have implemented the following so far.
My main (UI) thread spawns a thread that creates a new window that contains the control in question, and locates it in the correct position (so that it looks like it is just a normal control).

_leftTopThread = new Thread(() =>
{
   _topLeftMap = new MapWindow()
   {
      WindowStartupLocation = WindowStartupLocation.Manual,
      Width = leftLocation.Width,
      Height = leftLocation.Height,
      Left = leftLocation.X,
      Top = leftLocation.Y,
      CommandQueue = _leftMapCommandQueue,
   };

   _topLeftMap.Show();
   System.Windows.Threading.Dispatcher.Run();

});

_leftTopThread.SetApartmentState(ApartmentState.STA);
_leftTopThread.IsBackground = true;
_leftTopThread.Name = "LeftTop";
_leftTopThread.Start();

Where CommandQueue is a Thread-safe BlockingCollection Queue for sending commands to the map (moving the location, etc).
The problem is now that I can either

  • have user input due to the System.Windows.Threading.Dispatcher.Run() call
  • or block on the CommandQueue, listening for commands sent by the main thread

I can’t spin waiting for commands, because it would soak up all my thread CPU!
Is it possible to block and have the event message-pump working?

  • 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-01T21:36:27+00:00Added an answer on June 1, 2026 at 9:36 pm

    Well, I have a method that works – but it may well not be the most elegant..

    I have a window that contains my third party (slow-rendering) control in the XAML.

    public partial class MapWindow : Window
    {
        private ConcurrentQueue<MapCommand> _mapCommandQueue;
        private HwndSource _source;
    
        // ...
    
    }
    

    My main (UI) thread contructs and starts this window on a thread:

    _leftTopThread = new Thread(() =>
    {
       _topLeftMap = new MapWindow()
       {
          WindowStartupLocation = WindowStartupLocation.Manual,
          CommandQueue = _leftMapCommendQueue,
       };
    
        _topLeftMap.Show();
        System.Windows.Threading.Dispatcher.Run();
    
    });
    
    _leftTopThread.SetApartmentState(ApartmentState.STA);
    _leftTopThread.IsBackground = true;
    _leftTopThread.Name = "LeftTop";
    _leftTopThread.Start();
    

    I then get a handle to the window in the thread (after it has initialised):

    private IntPtr LeftHandMapWindowHandle
    {
        get
        {
            if (_leftHandMapWindowHandle == IntPtr.Zero)
            {
                if (!_topLeftMap.Dispatcher.CheckAccess())
                {
                    _leftHandMapWindowHandle = (IntPtr)_topLeftMap.Dispatcher.Invoke(
                      new Func<IntPtr>(() => new WindowInteropHelper(_topLeftMap).Handle)
                    );
                }
                else
                {
                    _leftHandMapWindowHandle = new WindowInteropHelper(_topLeftMap).Handle;
                }
            }
            return _leftHandMapWindowHandle;
        }
    }
    

    .. and after putting a command onto the thread-safe queue that is shared with the threaded window:

    var command = new MapCommand(MapCommand.CommandType.AircraftLocation, new object[] {RandomLatLon});
    _leftMapCommendQueue.Enqueue(command);
    

    .. I let it know it can check the queue:

    PostMessage(LeftHandMapWindowHandle, MapWindow.WmCustomCheckForCommandsInQueue, IntPtr.Zero, IntPtr.Zero);
    

    The window can receive my message because it has hooked into the window messages:

    protected override void OnSourceInitialized(EventArgs e)
    {
        base.OnSourceInitialized(e);
    
        _source = PresentationSource.FromVisual(this) as HwndSource;
        if (_source != null) _source.AddHook(WndProc);
    }
    

    ..which it then can check:

    private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled) // 
    {
        // Handle messages...
        var result = IntPtr.Zero;
    
        switch (msg)
        {
            case WmCustomCheckForCommandsInQueue:
                CheckForNewTasks();
                break;
    
        }
        return result;
    }
    

    ..and then execute on the thread!

    private void CheckForNewTasks()
    {
        MapCommand newCommand;
        while (_mapCommandQueue.TryDequeue(out newCommand))
        {
            switch (newCommand.Type)
            {
                case MapCommand.CommandType.AircraftLocation:
                    SetAircraftLocation((LatLon)newCommand.Arguments[0]);
                    break;
    
                default:
                    Console.WriteLine(String.Format("Unknown command '0x{0}'for window", newCommand.Type));
                    break;
            }
        }
    }
    

    Easy as that.. 🙂

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

Sidebar

Related Questions

I have just started using TFS2010 as a source control, moving away from VSS.
Using asp.net 3.5 Gridview control, visual studio 2008. I have played with all the
I have a .NETCF project that I have been trying to convert from .NETCF
I have a website project that is consuming user controls from another shared project.
I have been using the embedded server that visual studio has to test my
I have this Visual Studio solution that includes a project that has a template
In my project I am using a custom usercontrol that inherits from devexpress gridcontrol.
I have a project that I am trying to fix from a guy that
I created an ActiveX control ocx file using Visual Studio 2008. Then tried registering
Friends, I'm using a datagridview control in my visual studio 2005 windows application. Here

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.