zoukankan      html  css  js  c++  java
  • Windows 8.1 开发过程中遇到的小问题(2)

    又是在Windows 8.1 的分享功能,再次出现错误:

    A COM call (IID: ***, method index: *) to an ASTA (thread *) was blocked because the call chain originated in or passed through another ASTA (thread *). This call pattern is deadlock-prone and disallowed by apartment call control.

    Additional information: A COM call to an ASTA was blocked because the call chain originated in or passed through another ASTA. This call pattern is deadlock-prone and disallowed by apartment call control.

    A COM call (IID: {*}, method index: *) to an ASTA (thread *) was blocked because the call chain originated in or passed through another ASTA (thread *). This call pattern is deadlock-prone and disallowed by apartment call control.

    问题代码,红色部分是错误源:(作为分享目标应用,接受分享进来的文件的代码:例如,用photo应用分享图片)

    else if (containsStorageFileFormat)
                {
                    try
                    {
                        var items = await operation.Data.GetStorageItemsAsync();
                        Windows.Storage.StorageFile sf = items[0] as Windows.Storage.StorageFile;
                        if (sf.FileType == ".jpg" || sf.FileType == ".png" || sf.FileType == ".gif")
                        {
                            using (IRandomAccessStreamWithContentType ras = await sf.OpenReadAsync())
                            {                            
                                ras.Seek(0);
                                if (vm.UploadImage == null)
                                    vm.UploadImage = new BitmapImage();
                                vm.UploadImage.SetSource(ras);
                                using (System.IO.Stream stream = ras.AsStreamForRead())
                                {
                                    stream.Seek(0, SeekOrigin.Begin);
                                    using (System.IO.MemoryStream memStream = new System.IO.MemoryStream())
                                    {
                                        stream.CopyTo(memStream);
                                        memStream.Position = 0;
    
                                        vm.SetImageRelative(memStream, "ShareImage.jpg", ras.ContentType, true);
                                    }
                                }
                            }
                        }
                    }
                    catch (Exception e2)
                    {
                        WeiboForWin8.Tools.MainProjectTool.HandleException(e2, "ShareTargetView.OnNavigatedTo_2");
                        vm.UploadImage = null;
                    }
                }

    以下是来自http://www.tuicool.com/articles/Rjmeay 帖子中对ASTA 和 STA 的描述,

     “Application Single Threaded Apartment” (ASTA).

    MSDN Reference on Application Single Threaded Apartments

    Which, essentially says that an ASTA is an STA that you can’t create yourself and which doesn’t allow re-entrancy.

    也就是说,线程访问出现了问题,后把此行代码改为异步调用:

    else if (containsStorageFileFormat)
                {
                    try
                    {
                        var items = await operation.Data.GetStorageItemsAsync();
                        Windows.Storage.StorageFile sf = items[0] as Windows.Storage.StorageFile;
                        if (sf.FileType == ".jpg" || sf.FileType == ".png" || sf.FileType == ".gif")
                        {
                            IRandomAccessStreamWithContentType ras = null;
                            await Task.Run(async() =>
                            {
                                ras = await sf.OpenReadAsync();                            
                            });
                            if (ras != null) { 
                                    ras.Seek(0);
                                    if (vm.UploadImage == null)
                                        vm.UploadImage = new BitmapImage();
                                    vm.UploadImage.SetSource(ras);
                                    using (System.IO.Stream stream = ras.AsStreamForRead())
                                    {
                                        stream.Seek(0, SeekOrigin.Begin);
                                        using (System.IO.MemoryStream memStream = new System.IO.MemoryStream())
                                        {
                                            stream.CopyTo(memStream);
                                            memStream.Position = 0;
    
                                            vm.SetImageRelative(memStream, "ShareImage.jpg", ras.ContentType, true);
                                        }
                                    }
                            }
                        }
                    }
                    catch (Exception e2)
                    {
                        WeiboForWin8.Tools.MainProjectTool.HandleException(e2, "ShareTargetView.OnNavigatedTo_2");
                        vm.UploadImage = null;
                    }
                }

    大功告成。

    我在做share target的功能的时候,发现很多问题,比如:应用程序未启动或被结束进程时,分享中的某些方法调用正常,但程序在进程中时会发生异常,反之也会有异常,可能是某些方法调用的机制不同(未启动时只能通过COM?),因此,大家一定要在做此功能的时候注意代码的调式和错误处理,尽管很繁琐(调式share简直就是一种煎熬)

  • 相关阅读:
    「网络流 24 题」魔术球
    「网络流 24 题」圆桌聚餐
    「网络流 24 题」最小路径覆盖
    「网络流 24 题」太空飞行计划
    「网络流 24 题」搭配飞行员
    「网络流 24 题」负载平衡
    「网络流 24 题」数字梯形
    餐巾计划问题
    让Double类型完整显示,不用科学计数法显示E
    kotlin新工程
  • 原文地址:https://www.cnblogs.com/lihaiyin/p/3457164.html
Copyright © 2011-2022 走看看