The calling thread cannot access this object because a different thread owns it.
The exception message stated above occurs generally when UI controls are accessed by other threads rather than the UI thread itself.I came through this issue.As far my experience,i faced this error while i tried to access the UI from a BackGroundWorker
's DoWork
event.When i became aware of the fact that the UI cannot be accessed from a different thread rather than the UI thread,i switched to other methods to tackle such a situation by doing some HEAVY NON-UI work in DoWork
event.Today,i thought of doing something different(which may be not different to u as u may have done this before).I tried to do the UI THINGS in DoWork
event in a very different way.
Before i post my huge code,excuse me to explain a bit please.
My motive was to load a few images in some Image
control from a database/datatable in WPF.This takes some time as there are more than just a few images in the database/datatable.It results in UI-Freeze.So i though if declaring a Class level List(of BitmapImage)
variable,add images to that and then , in RunWorkerCompleted
event,i thought of doing a For Each
loop and load images in Image
controls.
Before i would test it, i tried implementing this using a database with single image and only a single Image
control.
Here's how i add images:
public class TestSub
{
BitmapImage CurrentUserPhoto = new BitmapImage;
private void LoadAccounts_DoWork(object sender, DoWorkEventArgs e)
{
con.Open();
OleDbCommand cmd2 = new OleDbCommand("Select * from userinfo where Email='" + currentemail + "'", con);
byte[] photo;
OleDbDataReader dr2 = cmd2.ExecuteReader;
while (dr2.Read)
{
try
{
photo = (byte[])dr2(11);
MemoryStream strm = new MemoryStream(photo);
BitmapImage bi = new BitmapImage;
bi.BeginInit();
bi.CacheOption = BitmapCacheOption.OnLoad;
bi.StreamSource = strm;
bi.EndInit();
CurrentUserPhoto = bi;
}
catch (Exception ex)
{
}
if (dr2(9).ToString == "")
{
}
else
{
CurrentUser = dr2(9);
}
}
con.Close();
}
}
Then,in RunWorkerCompleted
, i try to load that image :
private void LoadCurrentAccount_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
CurrentUserPhoto.BeginInit();
userpic.ImageSource = CurrentUserPhoto;
CurrentUserPhoto.EndInit();
CurrentUserName.Content = CurrentUser;
}
BAMMM!The error occurs!! In the first line CurrentUserPhoto.BeginInit();
. I am not getting it as i am(as far my knowledge) not tying to access the UI from a different thread(i mean,i ain't accessing it from DoWordk
event).
So,can somebody please explain to me why this error is occuring ?