This is a migrated thread and some comments may be shown as answers.

RadGuage - Thread Cannot be accessed

3 Answers 119 Views
Gauges
This is a migrated thread and some comments may be shown as answers.
Bill
Top achievements
Rank 1
Bill asked on 23 Mar 2009, 04:54 AM
Hello,

I have successfully created the guage that I want to use a design-time in XAML. However, I have a need to create the guage at runtime. Everytime that I try to add a RadialScale to a Grid, WPF throws an Exception that says:

The calling thread cannot access this object because a different thread owns it.

What am I doing wrong? Here is my code:

// Setup the grid that the gauge will be defined in
System.Windows.Controls.Grid gaugeGrid = new System.Windows.Controls.Grid();
gaugeGrid.Height = 140;
gaugeGrid.Width = 140;
gaugeGrid.Margin = new Thickness(0, 20, 0, 0);

// Define the radial scale with the Telerik control
RadialScale radialScale = new RadialScale();
radialScale.Min = 40;
radialScale.Max = 60;
radialScale.MinorTicks = 2;
radialScale.MajorTicks = 6;

// Exception gets thrown here
gaugeGrid.Children.Add(radialScale);

3 Answers, 1 is accepted

Sort by
0
Andrey
Telerik team
answered on 23 Mar 2009, 08:16 AM
Hello Bill,

Unfortunately the provided code snippet is not sufficient to pinpoint the problem. I’ve checked your code in very simple WPF application and it works just fine. Here it is full code I’ve used:

[XAML]
<Window x:Class="Telerik.WPF.Gauge.Window1" 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation" 
        Title="Window1" Height="400" Width="500">  
    <Grid Name="layoutRoot">  
    </Grid> 
</Window> 

[C#]
using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
using System.Windows;  
using System.Windows.Controls;  
using System.Windows.Data;  
using System.Windows.Documents;  
using System.Windows.Input;  
using System.Windows.Media;  
using System.Windows.Media.Imaging;  
using System.Windows.Navigation;  
using System.Windows.Shapes;  
using Telerik.Windows.Controls.Gauges;  
 
namespace Telerik.WPF.Gauge  
{  
    /// <summary>  
    /// Interaction logic for Window1.xaml  
    /// </summary>  
    public partial class Window1 : Window  
    {  
        public Window1()  
        {  
            InitializeComponent();  
 
            // Setup the grid that the gauge will be defined in  
            System.Windows.Controls.Grid gaugeGrid = new System.Windows.Controls.Grid();  
            gaugeGrid.Height = 140;  
            gaugeGrid.Width = 140;  
            gaugeGrid.Margin = new Thickness(0, 20, 0, 0);  
 
            // Define the radial scale with the Telerik control  
            RadialScale radialScale = new RadialScale();  
            radialScale.Min = 40;  
            radialScale.Max = 60;  
            radialScale.MinorTicks = 2;  
            radialScale.MajorTicks = 6;  
 
            // Exception gets thrown here  
            gaugeGrid.Children.Add(radialScale);  
 
            layoutRoot.Children.Add(gaugeGrid);  
        }  
    }  
}  
 

The “The calling thread cannot access this object because a different thread owns it.” Is thrown usually when UE elements (like Grid or RadialScale) are used from thread different from UI one.Do you use your code to create RadialScale in the separate thread? If it is so, then it will not work. You should call all UI relative code from the UI thread. For example:
private void NotUIThreadRunMethod()  
{  
    // ...  
 
    // Create Radial Scale Control from thread different  
    // from UI thread. This will work as its using the dispatcher.  
    Dispatcher.Invoke(DispatcherPriority.Normal,  
                      new Action(CreateRadialScale));  
 
    // ...  
}  
 
private void CreateRadialScale()  
{  
    // Setup the grid that the gauge will be defined in  
    System.Windows.Controls.Grid gaugeGrid = new System.Windows.Controls.Grid();  
    gaugeGrid.Height = 140;  
    gaugeGrid.Width = 140;  
    gaugeGrid.Margin = new Thickness(0, 20, 0, 0);  
 
    // Define the radial scale with the Telerik control  
    RadialScale radialScale = new RadialScale();  
    radialScale.Min = 40;  
    radialScale.Max = 60;  
    radialScale.MinorTicks = 2;  
    radialScale.MajorTicks = 6;  
 
    // Exception gets thrown here  
    gaugeGrid.Children.Add(radialScale);  
 
    layoutRoot.Children.Add(gaugeGrid);  
}  
 


All the best,
Andrey Murzov
the Telerik team

Check out Telerik Trainer , the state of the art learning tool for Telerik products.
0
Frustrated Dev
Top achievements
Rank 1
answered on 26 Mar 2009, 07:20 PM
I have the same problem. My code to create the RadialScale is being used in a seperate thread. I'm not sure why this wouldn't work though. Is there a work around? The reason I'm not sure why this wouldn't work is because I can generate other WPF controls in the thread I am using. In fact, I have noticed that this code works fine on the first time through. However, this error gets thrown on subsequent calls. That would imply that something that should be closed/freed is not be closed or freed. Is there a bug in the RadialScale / Needle controls? The code works fine if I do not add the Telerik controls to the Grid. However, when I add the Telerik controls to the Grid, my application starts throwing this error.
0
Andrey
Telerik team
answered on 27 Mar 2009, 02:35 PM

Hello Bill,
This problem is not related to the RadGauge control, nor any other control. The WPF threading model has limitations on using GUI controls in threads different from main UI thread. I would recommend an MSDN article on the WPF threading model and it's restrictions:
http://msdn.microsoft.com/en-us/magazine/cc163328.aspx

The solution is to use Dispatcher.Invoke or Dispatcher.BeginInvoke methods in your thread to call the methods which create GUI control (RadGauge, for example) in the GUI thread. You can find detailed description of this approach in my previous post or in the MSDN article provided above.

All the best,
Andrey Murzov
the Telerik team


Check out Telerik Trainer , the state of the art learning tool for Telerik products.
Tags
Gauges
Asked by
Bill
Top achievements
Rank 1
Answers by
Andrey
Telerik team
Frustrated Dev
Top achievements
Rank 1
Share this question
or