Invalid scheduling agent cross-exclusion when working with isolated storage

I work with the Windows Phone Schedule Agent and I try to update the image name after synchronization, the problem is that I get an invalid cross exception when this function is in the 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
source share
1 answer

The problem arises because BitmapImage cannot be created outside the UI thread. You can fix this problem by wrapping calls in a Dispatcher Invoke call.

However, you need to make sure that you correctly call NotifyComplete. In this case, you may need to add NotifyComplete to the Dispatcher call.

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

All Articles