Domanda

I'm building a WP8 application that changes the background of the lockscreen using images from the Internet. I followed the tutorials over Scheduled Agents and Lockscreen, but I have a problem.

When I try to download the new background image from the Scheduled Agent, i get this:

+       $exception  {System.UnauthorizedAccessException: Invalid cross-thread access.
   at MS.Internal.XcpImports.CheckThread()
   at System.Windows.DependencyObject..ctor(UInt32 nativeTypeIndex, IntPtr constructDO)
   at System.Windows.Media.Imaging.BitmapImage..ctor()
   at TileLockAgent.ScheduledAgent.lockScreenClient_OpenReadCompleted(Object sender, OpenReadCompletedEventArgs e)
   at System.Net.WebClient.OnOpenReadCompleted(OpenReadCompletedEventArgs e)
   at System.Net.WebClient.OpenReadOperationCompleted(Object arg)
   at System.Threading.WaitCallback.Invoke(Object state)
   at System.Threading.QueueUserWorkItemCallback.WaitCallback_Context(Object state)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
   at System.Threading.ThreadPoolWorkQueue.Dispatch()
   at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()}   System.Exception {System.UnauthorizedAccessException}

The code is:

string fileName;

try
{
    var currentImage = LockScreen.GetImageUri();

    if (currentImage.ToString().EndsWith("_1.jpg"))
    {
        fileName = "LockBackground_2.jpg";
    }
    else
    {
        fileName = "LockBackground_1.jpg";
    }
}
catch
{
    // lockscreen not set or prev owned by other app          
    fileName = "LiveLockBackground_1.jpg";
}

using (var isoStore = IsolatedStorageFile.GetUserStoreForApplication())
{
    var bi = new BitmapImage();
    bi.SetSource(e.Result);
    var wb = new WriteableBitmap(bi);
    using (var isoFileStream = isoStore.CreateFile(fileName))
    {
        var width = wb.PixelWidth;
        var height = wb.PixelHeight;
        Extensions.SaveJpeg(wb, isoFileStream, width, height, 0, 100);
    }
}

I really don't know how to solve this. How can I save an image in a scheduled agent if BitmapImage is not working? What does it mean that i'm doing "cross-thread access"? The images are created and used only by the scheduled agent, so no one should be accessing them.

È stato utile?

Soluzione

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();
    });

Source:Invalid Cross Exception on Schedule Agent when working on isolated storage

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top