Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: WPF Threading
Dear Expert,
I'm writing an application, where I'm calling a method in a new thread. In the method i'm doing some task. On completion of task i'm sending an event to UI (update UI control). But while updating UI control, i'm getting below exception "The calling thread cannot access this object because a different thread owns it.;"
 
In my UI control I've two button , button1 and button2 and a treeview treeView1. On clicking button1 i'm calling the thread method. Below is my code. Please let me know what is the wrong I'm doing in my code.
 
Mainwindow.cs
 
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 System.Threading;
 
namespace AddDelete
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        Thread      thread              = null;
        ThClass     objThClass          = null;
 
        public MainWindow()
        {
            InitializeComponent();
        }
 
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            for (int i = 1; i <= 10; ++i)
            {
                treeView1.Items.Add(i.ToString());
            }
 
            objThClass = new ThClass();
 
            if(null == thread)
                thread = new Thread(new ThreadStart(objThClass.TestMethode));
 
            thread.Start();
 
            objThClass.Changed += TempHandler;
        }
 
        private void button2_Click(object sender, RoutedEventArgs e)
        {
            treeView1.Items.Clear();
        }
 
        public void TempHandler(object sender, EventArgs e)
        {
            try
            {
                //treeView1.Items.Clear();

                treeView1.Items.Add("Temp1");
                
                thread.Abort();
                thread = null;
            }
            catch (Exception exp)
            {
                Console.WriteLine(exp);
            }
        }
    }
}
 

ThClass.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
using System.Threading;
 
namespace AddDelete
{
    public delegate void ChangedEventHandler(object sender, EventArgs e);
 
    class ThClass
    {
        public event ChangedEventHandler Changed;
 
        public void TestMethode()
        {
            // Do some task (DB operation)
            // On task complete
            Changed(this, null);
        }
    }
}
 

 

Thank you all.
Posted 21-May-12 4:29am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

  Permalink  
Comments
vidiking at 21-May-12 9:55am
   
No. This is not working with my main code.
Error - Exception has been thrown by the target of an invocation.
Shmuel Zang at 21-May-12 10:29am
   
What's the code that cause that exception? What's the inner exception? (The thread.Abort(); line causes a ThreadAbortException on the thread...) Just run the treeView1.Items.Add("Temp1"); line (in your case), using the Dispatcher.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Check out the first answer here:
http://social.msdn.microsoft.com/forums/en-US/wpf/thread/360540eb-d756-4434-86f9-a3449f05eb55[^]
 
Should be the same, just replace TextBox with your TreeView.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 240
1 Kamal Rocks 184
2 PIEBALDconsult 150
3 BillWoodruff 148
4 Jochen Arndt 135
0 OriginalGriff 5,695
1 DamithSL 4,506
2 Maciej Los 4,007
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,190


Advertise | Privacy | Mobile
Web02 | 2.8.141216.1 | Last Updated 21 May 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100