因为不同的线程拥有it.How做我编辑的图像调用线程不能访问此对象吗? [英] The calling thread cannot access this object because a different thread owns it.How do i edit the image?

查看:77
本文介绍了因为不同的线程拥有it.How做我编辑的图像调用线程不能访问此对象吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有很多这种类型的问题的。从DB我想后,这样我可以分享我的具体概率,因为IM感到沮丧。



IM正在运行的线程它查询路径,并把它在图像element.problem是的,我在XAML中创建的图像,所以当我运行这个线程,它抛出了不能访问它不能访问图像元素此对象错误。



那我怎么设置它不使用XAML?这里是我的代码片段:

 公共部分类窗口1:窗口
{


螺纹Frame1中;

公共窗口1()
{
的InitializeComponent();
intializeDb();
#REGION起始帧1线程
Frame1中=新主题(帧1);
Frame1.SetApartmentState(ApartmentState.STA);
Frame1.IsBackground = TRUE;
Frame1.Start();
#endregion

}

公共无效帧1()
{
线K表;

command.CommandText =SELECT * FROM imageframe1
sqlConn.Open();
=读者Command.ExecuteReader却();

,而(Reader.Read())
{
BitmapImage的LOGO =新的BitmapImage();
logo.BeginInit();
k的序列=(串)(Reader.GetValue(1));
logo.UriSource =新的URI(K);
logo.EndInit();
image1.Source =标识; //引发错误HERE.IT CANT ACCESS此搜索
Thread.sleep代码(1000);
}
sqlConn.Close();
Reader.Close();

}



如何将我访问此搜索呢?如果我创建的线程中一个新的,我将不得不把作为面板的儿童,则IM会得到它不能访问面板错误。



解决这个办法很高兴,如果有人可以编写基于我的片段为例



与仍然没有成功编辑和制作同样的错误:

 公共部分类窗口1:窗口
{
公共只读的SynchronizationContext mySynchronizationContext;

公共窗口1()
{
的InitializeComponent();

mySynchronizationContext = SynchronizationContext.Current;
Frame1中=新主题(帧1);
Frame1.SetApartmentState(ApartmentState.STA);
Frame1.IsBackground = TRUE;
Frame1.Start();
}

公共无效帧1()
{
线K表;

command.CommandText =SELECT * FROM imageframe1
sqlConn.Open();
=读者Command.ExecuteReader却();



,而(Reader.Read())
{
BitmapImage的LOGO =新的BitmapImage();
logo.BeginInit();
k的序列=(串)(Reader.GetValue(1));
logo.UriSource =新的URI(K);
logo.EndInit();
SendOrPostCallback回调= _ =>
{
image1.Source =标识;
};

mySynchronizationContext.Send(回调,NULL);

//image1.Source =标识;
Thread.sleep代码(1000);
}
sqlConn.Close();
Reader.Close();

}
}


解决方案

由于乔恩斯基特说,你可以使用 Dispatcher.Invoke 的图像分配,但是这还不够,因为的BitmapImage 已在另一个线程创建的。为了能够使用它在UI线程上,你需要冻结之前:

  logo.Freeze(); 
行动对行动= {委托= image1.Source标志; };
image1.Dispatcher.Invoke(动作);


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.

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();

    }

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.

edited with still no success and producing the same error:

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();

    }
}

解决方案

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.How做我编辑的图像调用线程不能访问此对象吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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