Fixing reading XPS memory leak issue

  class Program
    {
      
        static int intCounter = 0;
        static object _intLock = new object();
        static int getInt()
        {
            lock (_intLock)
            {
                return intCounter++;
            }
        }

        static void Main(string[] args)
        {

            Console.ReadLine();

            while (true)
            {
                while (!Console.KeyAvailable)
                {
                    for (int i = 0; i < 100; i++)
                    {
                        Thread t = new Thread(() =>
                          {
                              var ogXps = File.ReadAllBytes(@"C:\Users\nathan\Desktop\hahaha.xps");
                              var returnedDoc = Test(ogXps);
                              string id = getInt().ToString();
                            //    File.WriteAllBytes(@".\output\" + id + ".xps", returnedDoc);
                            Console.WriteLine(id);
                          });
                        t.SetApartmentState(ApartmentState.STA);

                        t.Start();
                        t.Join();
                    }
                    Console.ReadLine();
                }
                Console.ReadLine();
            }
            Console.WriteLine("All done.");

        }



        static byte[] Test(byte[] originalXPS)
        {

            try
            {
         

                MemoryStream inputStream = new MemoryStream(originalXPS);

                string memoryStreamUri = "memorystream://" + Path.GetFileName(Guid.NewGuid().ToString() + ".xps");
                Uri packageUri = new Uri(memoryStreamUri);
                Package oldPackage = Package.Open(inputStream);
                PackageStore.AddPackage(packageUri, oldPackage);
                XpsDocument xpsOld = new XpsDocument(oldPackage, CompressionOption.Normal, memoryStreamUri);
                FixedDocumentSequence seqOld = xpsOld.GetFixedDocumentSequence();
               
                byte[] ret = originalXPS;

              

                PackageStore.RemovePackage(packageUri);
                xpsOld.Close();
                oldPackage.Close();
                //The following did solve some of the memory issue
                //-----------------------------------------------
                var docPager = seqOld.DocumentPaginator;
                docPager.ComputePageCount();
                for (int i = 0; i < docPager.PageCount; i++)
                {
                    FixedPage fp = docPager.GetPage(i).Visual as FixedPage;
                    fp.UpdateLayout();
                }
                seqOld = null;
                //-----------------------------------------------    
                Dispatcher.CurrentDispatcher.InvokeShutdown();
                
                return ret;
            }
            catch (Exception e)
            {
             //   log.Error("Error adding watermark.  Returning original bytes.  Details: " + e.ToString());
                return originalXPS;
            }
        }


    }

留言

這個網誌中的熱門文章

msdeploy include ApplicationPool setting and ignore recycle time

Invoke generic method