简体   繁体   中英

BackgroundWorker - Calling thread cannot access object

So in my DoWork, I have

List<KeyValuePair<String, BitmapImage>> Files = new List<KeyValuePair<string,BitmapImage>>((List<KeyValuePair<String, BitmapImage>>)e.Argument);

foreach (KeyValuePair<String, BitmapImage> i in Files)
{
    string temp = i.ToString();               .......(1)
    //.....
}

I'm passing the argument as follows :-

backgroundWorkerForReupload.RunWorkerAsync(files);

where "files" is of Type List<KeyValuePair<string, BitmapImage>>

At line (1) above, it throws an InvalidOperationException with the message : " The calling thread cannot access this object because a different thread owns it. "

I'm confused as to why i is owned by another thread. Any help would be greatly appreciated. Thank you.

Most WPF objects, including BitmapImage , are thread-affine, and can only be used by the thread they were created on.

You can fix this for BitmapImage by calling Freeze() first.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM