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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T11:41:10+00:00 2026-06-07T11:41:10+00:00

I have a side-window that is opened when clicking a button on the main

  • 0

I have a side-window that is opened when clicking a button on the main window. This side-window is created on a separate thread from the main window.

When I click the button the first time, everything runs fine. Then I close the side-window. Now when I click the button a second time, it throws the InvalidOperationException.

When the side-window is closed, it and the thread it runs on are discarded, and each time it is opened, an entirely new thread is created which creates an entirely new window. The exception is raised in the constructor of the lowest sub-element of the window (i.e. the first time a UI object is accessed in constructing the window).

Here is what happens. The handler of the button click:

public partial class MainWindow : Window
{
    private void OnVariableMonitoringButtonClick(object sender, RoutedEventArgs e)
    {
        if (monitoringWindow == null)
        {
            Cursor = Cursors.Wait;
            monitoringWindow = new MonitoringWindowWrapper();
            monitoringWindow.Loaded += OnMonitoringWindowLoaded;
            monitoringWindow.Closed += OnMonitoringWindowClosed;
            monitoringWindow.Start(); // <---
        }
        else
        {
            monitoringWindow.Activate();
        }
        e.Handled = true;
    }

    void OnMonitoringWindowLoaded(object sender, EventArgs e)
    {
        Dispatcher.Invoke(new Action(() => Cursor = Cursors.Arrow));
    }

    void OnMonitoringWindowClosed(object sender, EventArgs e)
    {
        monitoringWindow = null;
        Dispatcher.Invoke(new Action(() => Cursor = Cursors.Arrow));
    }
}

The MonitoringWindowWrapper class merely wraps the relevant methods of the MonitoringWindow class inside Dispatcher invocations to handle cross-thread calls.

The code enters the MonitoringWindowWrapper.Start() method:

partial class MonitoringWindowWrapper
{
    public void Start()
    {
        // Construct the window in a parallel thread
        Thread windowThread = new Thread(LoadMonitoringWindow);
        windowThread.Name = "Monitoring Window Thread";
        windowThread.IsBackground = true;
        windowThread.SetApartmentState(ApartmentState.STA);
        windowThread.Start(); // <---
    }

    private void LoadMonitoringWindow()
    {
        try
        {
            // Construct and set up the window
            monitoringWindow = new MonitoringWindow(); // <---
            monitoringWindow.Loaded += OnMonitoringWindowLoaded;
            monitoringWindow.Closed += OnMonitoringWindowClosed;
            monitoringWindow.Show();

            // Start window message pump on this thread
            System.Windows.Threading.Dispatcher.Run();
        }
        catch (Exception e)
        {
            // Catch any exceptions in this window to save the application from crashing
            ErrorMessasgeBox.Show(e.Message);
            if (Closed != null) Closed(this, new EventArgs());
        }
    }
}

The code then enters the MonitoringWindow.MonitoringWindow() constructor and filters down to my lowest sub-element in the window:

public partial class MonitoringWindow : Window
{
    public MonitoringWindow()
    {
        InitializeComponent(); // <---
        // ...
    }
}

All the way down to:

public partial class LineGraph : UserControl, IGraph, ISubGraph
{

    public LineGraph()
    {
        InitializeComponent();
        Brush b = GraphUtilities.BackgroundGradient;
        Background = b; // EXCEPTION THROWN AT THIS LINE
        BorderBrush = GraphUtilities.Border;
    }
}

The exception call stack may give some insight into where the exception is thrown:

System.InvalidOperationException was unhandled by user code
 HResult=-2146233079
 Message=The calling thread cannot access this object because a different thread owns it.
 Source=WindowsBase
 StackTrace:
    at System.Windows.Threading.Dispatcher.VerifyAccess()
    at System.Windows.Freezable.ReadPreamble()
    at System.Windows.Media.GradientStopCollection.OnInheritanceContextChangedCore(EventArgs args)
    at System.Windows.DependencyObject.OnInheritanceContextChanged(EventArgs args)
    at System.Windows.DependencyObject.OnInheritanceContextChanged(EventArgs args)
    at System.Windows.Freezable.AddInheritanceContext(DependencyObject context, DependencyProperty property)
    at System.Windows.DependencyObject.ProvideSelfAsInheritanceContext(DependencyObject doValue, DependencyProperty dp)
    at System.Windows.DependencyObject.ProvideSelfAsInheritanceContext(Object value, DependencyProperty dp)
    at System.Windows.DependencyObject.UpdateEffectiveValue(EntryIndex entryIndex, DependencyProperty dp, PropertyMetadata metadata, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType)
    at System.Windows.DependencyObject.SetValueCommon(DependencyProperty dp, Object value, PropertyMetadata metadata, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType, Boolean isInternal)
    at System.Windows.DependencyObject.SetValue(DependencyProperty dp, Object value)
    at System.Windows.Controls.Control.set_Background(Brush value)
    at Graphing.LineGraph..ctor() in z:\Documents\Projects\Software\Serial\SerialWindows\Graphing\LineGraph\LineGraph.xaml.cs:line 28
    at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)

The only reasons that I can think of are following:
1. There is a property of an object created on another thread that is bound to the Background property of this LineGraph object? However there aren’t any such bindings (at least explicitly) in my application, and if that was the case then why does it work the first time?
2. The Background property is somehow not owned by the LineGraph object? But this makes no sense.
3. The constructor is somehow not being executed on the thread that created the object which it is constructing? This again makes no sense, and the Visual Studio debugger says that it is operating on the “Monitoring Window Thread” thread.

How can I fix this problem?

I could possibly use the Dispatcher to set the background, but that is extremely tedious for all the calls to UI elements in that class and all the others, and it is not actually fixing the cause of the problem.

Thank you very much!

  • 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-07T11:41:13+00:00Added an answer on June 7, 2026 at 11:41 am

    I would guess that the culprit is GraphUtilities.BackgroundGradient, but you have not listed the GraphUtilities class. Brushes are freezable objects.

    From Freezable Objects Overview on MSDN:

    A frozen Freezable can also be shared across threads, while an unfrozen Freezable cannot.

    So the first time you run, that brush is associated with the monitoring window thread. The next time you open that window, it is a new thread. You will have to call the Freeze method on the brush if you want to use it from that other thread.

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

Sidebar

Related Questions

I have a Window that serves as a dialog in a WPF application. This
In my WPF main window written in XAML, I have a side content presenter
I have following code i want that window opened uses Ext.layout.BorderLayout and also on
I have a panel that is docked to the right side of a windows
I'm working within a platform that I do not have server side access to
We have a client side application (Java/Swing) that we need an HTML rendering control
Suppose I have a client side app that sends out requests to a rails
I have a window that displays text. There are two parts to the text:
I have a side navigation bar and when you click the links it submits
I have a flex 3 application that creates an Image from a canvas which

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.