Invalid Cross Exception on Schedule Agent when working on isolated storage

丶灬走出姿态 提交于 2019-12-11 05:09:37

问题


I am working with a Windows Phone Schedule Agent and I am trying to update the picture name after sync the problem is that I am getting an invalid cross exception when on this function at line "BitmapImage bmp = new BitmapImage();" and really don’t understand why.

void UpdateSyncPictureName(int AsyncStatus, int AticketID, int AsyncID, int ApictureID, int TsyncStatus = 0, int TsyncID = 0)
    {
        string filename = AsyncStatus + "-" + AticketID + "-" + AsyncID + "-" + ApictureID;
        using (IsolatedStorageFile ISF = IsolatedStorageFile.GetUserStoreForApplication())
        {
            if (ISF.FileExists(filename))
            {

                BitmapImage bmp = new BitmapImage();
                using (IsolatedStorageFileStream isoStream =
                    ISF.OpenFile(filename, System.IO.FileMode.Open))
                {
                    bmp.SetSource(isoStream);
                }
                ISF.DeleteFile(filename);
                WriteableBitmap Wbmp = new WriteableBitmap(bmp);
                using (IsolatedStorageFileStream isoStream =
                ISF.OpenFile(TsyncStatus + "-" + AticketID + "-" + TsyncID + "-" + ApictureID, System.IO.FileMode.Create))
                {
                    Extensions.SaveJpeg(Wbmp, isoStream,
                        Wbmp.PixelWidth,
                        Wbmp.PixelHeight,
                        0, 100);
                }


            }
        }
    }

回答1:


The issue arises from the fact that BitmapImage cannot be instantiated outside of the UI thread. You can fix this issue by wrapping your calls in a Dispatcher Invoke call.

However, you need to make sure that you call NotifyComplete correctly. As such you may need to put NotifyComplete in the Dispatcher call.

Deployment.Current.Dispatcher.BeginInvoke(() =>
{
    UpdateSyncPictureName(...);
    NotifyComplete();
});


来源:https://stackoverflow.com/questions/12898018/invalid-cross-exception-on-schedule-agent-when-working-on-isolated-storage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!