Click here to Skip to main content
15,883,988 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

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 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
C#
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

 
Share this answer
 
Comments
vidiking 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 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.
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.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900