简体   繁体   English

从计划的代理保存图像时出现System.UnauthorizedAccessException

[英]System.UnauthorizedAccessException when saving image from Scheduled Agent

I'm building a WP8 application that changes the background of the lockscreen using images from the Internet. 我正在构建一个WP8应用程序,该应用程序使用来自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: 当我尝试从Scheduled Agent下载新的背景图像时,得到以下信息:

+       $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? 如果BitmapImage无法正常工作,如何将图像保存在预定的代理中? 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. 映像仅由计划的代理创建和使用,因此没有人可以访问它们。

The issue arises from the fact that BitmapImage cannot be instantiated outside of the UI thread. 该问题是由于无法在UI线程外部实例化BitmapImage引起的。 You can fix this issue by wrapping your calls in a Dispatcher Invoke call. 您可以通过将呼叫包装在Dispatcher Invoke呼叫中来解决此问题。

However, you need to make sure that you call NotifyComplete correctly. 但是,您需要确保正确调用NotifyComplete。 As such you may need to put NotifyComplete in the Dispatcher call. 因此,您可能需要将NotifyComplete放入Dispatcher调用中。

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

Source: Invalid Cross Exception on Schedule Agent when working on isolated storage 来源: 在隔离存储上工作时,日程表代理上的无效交叉异常

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 UWP C# System.UnauthorizedAccessException 保存 XML 文件时 - UWP C# System.UnauthorizedAccessException when saving XML file 尝试从网络获取文件时获取System.UnauthorizedAccessException - Getting System.UnauthorizedAccessException when trying to fetch files from network 尝试从LocalFolder打开文件时出现“ System.UnauthorizedAccessException” - 'System.UnauthorizedAccessException' when trying to open file from LocalFolder 从异步任务创建文件时出现System.UnauthorizedAccessException - System.UnauthorizedAccessException when creating a file from Async task 执行Excel Interop的C#控制台应用程序 - 按计划运行时失败任务-System.UnauthorizedAccessException - C# console app that does Excel Interop - failing when running as scheduled Task -System.UnauthorizedAccessException 获取文件时出现System.UnauthorizedAccessException - System.UnauthorizedAccessException when getting files 获取目录时System.UnauthorizedAccessException - System.UnauthorizedAccessException when getting directories 通过链接启动应用程序时出现System.UnauthorizedAccessException - System.UnauthorizedAccessException when launch application by link 写入安装路径时出现System.UnauthorizedAccessException - System.UnauthorizedAccessException when writing to the install path 上载文件时出现System.UnauthorizedAccessException - System.UnauthorizedAccessException when uploading a file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM