更新线程任务的用户界面完成,提示错误. [英] Update UI on thread task complete giving error.

查看:91
本文介绍了更新线程任务的用户界面完成,提示错误.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尊敬的专家,
我正在编写应用程序,在新线程中调用方法.在方法上,我正在做一些任务.完成任务后,我将向UI发送事件(更新UI控件).但是在更新UI控件时,我遇到了以下异常:调用线程无法访问此对象,因为其他线程拥有它.;"

在我的UI控件中,我有两个按钮,button1和button2以及一个树视图treeView1.在单击button1时,我正在调用线程方法.下面是我的代码.请让我知道我在代码中做错了什么.

Mainwindow.cs

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




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.

推荐答案

在这里查看我的解决方案:

See my solution here:


在此处查看第一个答案:
http://social.msdn.microsoft.com/论坛/en-US/wpf/thread/360540eb-d756-4434-86f9-a3449f05eb55 [
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.


这篇关于更新线程任务的用户界面完成,提示错误.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆