private void RefreshOneThumbnailCallback(IPage page, Image image)
{
if (IsDisposed)
return;
// This guards against a bizarre situation we don't fully understand which arises when switching UI language (BL-221).
// In this situation, it appears that the ThumbnailList is in some sort of zombie state...its _listView has a handle,
// but it doesn't. No handle causes InvokeRequired to return false, but the ListView does require invoke and throws
// when we try to do anything with it.
// We tried a version that used Invoke on the listView, but got even weirder problems.
// Giving up when there is no handle doesn't seem to suppress thumbnail generation, so somehow things must get back
// into a good state.
// If you think of trying something else, please test carefully the scenario described in BL-221, and also that you can
// switch back to English without cross-thread exceptions being thrown.
if (!IsHandleCreated)
return;
if (InvokeRequired)
{
Invoke(new Action<IPage, Image>(RefreshOneThumbnailCallback), page, image);
return;
}
var imageIndex = _thumbnailImageList.Images.IndexOfKey(page.Id);
if (imageIndex > -1)
{
_thumbnailImageList.Images[imageIndex] = image;
//at one time the page we just inserted would have the same id, but be a different IPage object.
//Now, the above checks for id equality too (never did track down why the objects change, but this is robust, so I'm not worried about it)
var listItem = (from ListViewItem i in _listView.Items where ((i.Tag == page) || ((IPage)i.Tag).Id == page.Id) select i).FirstOrDefault();
if(listItem!=null)
{
_listView.Invalidate(listItem.Bounds);
}
else
{
Debug.Fail("Did not find a matching page."); //theoretically, this could happen if you managed to delete the page before its thumnbail could be built
var lastPage = _listView.Items[_listView.Items.Count - 1];
}
}
}