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简直就是一种煎熬)

  • 相关阅读:
    变分自编码器(Variational Autoencoder, VAE)通俗教程
    神经网络图灵机(Neural Turing Machines, NTM)
    【Java设计模式】单例模式
    SupportV7包中 SwipeRefreshLayout 修改下拉控件的距离
    eclipse项目导入androidstudio
    LiveWriter Test
    【Android学习】XML文本的三种解析方式(通过搭建本地的Web项目提供XML文件)
    【百度地图学习-一】初始化以及TextOverlay
    【Android学习】四种布局方式
    【Android学习】数据传递三种方式
  • 原文地址:https://www.cnblogs.com/lihaiyin/p/3457164.html
Copyright © 2011-2022 走看看