c# - The calling thread cannot access this object because a different thread owns it.How do i edit the image? -


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

im running thread query path db , put in image element.problem is, created image in xaml when run thread throws cannot access object error cant access image element.

then how set without using xaml??here's 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 * 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 error here.it cant access image1             thread.sleep(1000);         }         sqlconn.close();         reader.close();      } 

how access image1 then? if create new 1 within thread,i have put child of panel,an im gonna error cant access panel.

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

edited still no success , producing 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 * 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, can use dispatcher.invoke assign image, it's not enough, because bitmapimage has been created on thread. able use on ui thread, need freeze before:

logo.freeze(); action action = delegate { image1.source = logo; }; image1.dispatcher.invoke(action); 

Comments

Popular posts from this blog

c# - how to write client side events functions for the combobox items -

exception - Python, pyPdf OCR error: pyPdf.utils.PdfReadError: EOF marker not found -