调用线程无法访问此对象,因为其他线程拥有它.如何编辑图像? [英] The calling thread cannot access this object because a different thread owns it.How do i edit the image?

查看:35
本文介绍了调用线程无法访问此对象,因为其他线程拥有它.如何编辑图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有很多这类问题.我想发帖是为了分享我的具体问题,因为我很沮丧.

i know there's a lot of these type of questions. i wanted to post so that i can share my specific prob because im getting frustrated.

我正在运行一个线程,该线程从数据库查询路径并将其放入图像元素中..

im running a thread which query path from db and put it in the image element.problem is, i created the image in xaml so when i run this thread it throws the cannot access this object error which it cant access the image element.

那么我如何在不使用 xaml 的情况下设置它?这是我的代码片段:

then how do i set it without using xaml??here's my code snippet:

public partial class Window1 : Window
{


    Thread Frame1;

    public Window1()
    {
        InitializeComponent();
        intializeDb();
        #region start frame 1 thread
        Frame1 = new Thread(frame1);
        Frame1.SetApartmentState(ApartmentState.STA);
        Frame1.IsBackground = true;
        Frame1.Start();
        #endregion 

    }

public void frame1()
    {
        string k;

        command.CommandText = "SELECT * FROM imageframe1";
        sqlConn.Open();
        Reader = command.ExecuteReader();

        while (Reader.Read())
        {
            BitmapImage logo = new BitmapImage();
            logo.BeginInit();
            k = (string)(Reader.GetValue(1));
            logo.UriSource = new Uri(k);
            logo.EndInit();
            image1.Source = logo; //THROWS THE ERROR HERE.IT CANT ACCESS image1
            Thread.Sleep(1000);
        }
        sqlConn.Close();
        Reader.Close();

    }

那我将如何访问 image1 呢?如果我在线程中创建一个新的,我将不得不把它作为一个面板的子项,然后我会得到一个无法访问面板的错误.

how would i access image1 then? if i create a new one within the thread,i will have to put as child of a panel,an then im gonna get an error which it cant access the panel.

有什么办法可以解决这个问题吗?很高兴有人可以根据我的代码片段编写一个示例.

any way around this?glad if someone can write an example based on my snippet.

编辑仍然没有成功并产生同样的错误:

public partial class Window1 : Window
{
    public readonly SynchronizationContext mySynchronizationContext;

public Window1()
    {
        InitializeComponent();

        mySynchronizationContext = SynchronizationContext.Current;
        Frame1 = new Thread(frame1);
        Frame1.SetApartmentState(ApartmentState.STA);
        Frame1.IsBackground = true;
        Frame1.Start();
    }

public void frame1()
    {
        string k;

        command.CommandText = "SELECT * FROM imageframe1";
        sqlConn.Open();
        Reader = command.ExecuteReader();



        while (Reader.Read())
        {
            BitmapImage logo = new BitmapImage();
            logo.BeginInit();
            k = (string)(Reader.GetValue(1));
            logo.UriSource = new Uri(k);
            logo.EndInit();
            SendOrPostCallback callback = _ =>
            {
                image1.Source = logo;
            };

            mySynchronizationContext.Send(callback, null);

            //image1.Source = logo;
            Thread.Sleep(1000);
        }
        sqlConn.Close();
        Reader.Close();

    }
}

推荐答案

正如 Jon Skeet 所说,可以使用 Dispatcher.Invoke 来分配图片,但这还不够,因为 BitmapImage 已在另一个线程上创建.为了能够在 UI 线程上使用它,你需要先Freeze它:

As Jon Skeet said, you can use Dispatcher.Invoke to assign the image, but it's not enough, because the BitmapImage has been created on another thread. To be able to use it on the UI thread, you need to Freeze it before:

logo.Freeze();
Action action = delegate { image1.Source = logo; };
image1.Dispatcher.Invoke(action);

这篇关于调用线程无法访问此对象,因为其他线程拥有它.如何编辑图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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