If you close the app after using PdfDocument, the process will remain.

Asked 2 years ago, Updated 2 years ago, 505 views

I'd like to use the PdfDocument of WinRT in WPF, but there may be a process left even if I close the app.Is there anything missing?

Below is the reproduction code.

< u l >
  • PdfPage.RenderToStreamAsync() leaves the process.
  • VisualStudio 2022 debugging does not cause problems.Start without debugging
  • and reproduce.
  • GC.Collect() increases the reproduction rate of this symptom to nearly 100%.The association with the symptom is unknown.
  • Windows 10-64bit, .NET6, WPF environment.TargetFramework is net6.0-windows10.0.17763.0.
  • public partial class MainWindow:Window
        {
            public MainWindow()
            {
                InitializeComponent();
                Loaded + = MainWindow_Loaded;
            }
    
            private async void MainWindow_Loaded (object sender, RoutedEventArgse)
            {
                waitGetPdfPageStream(@"sample.pdf", 0);
                GC.Collect();
                This.Close();
            }
    
            private static async Task<MemoryStream>GetPdfPageStream (string path, uint index)
            {
                using var pdfStream=File.OpenRead(path);
                using var winrtStream=pdfStream.AsRandomAccessStream();
                varpdfDocument = wait PdfDocument.LoadFromStreamAsync (winrtStream);
                using var pdfPage=pdfDocument.GetPage(uint)index);
                varms = new MemoryStream();
                var outStream=ms.AsRandomAccessStream();
                wait pdfPage.RenderToStreamAsync (outStream);
                ms.Seek(0, SeekOrigin.Begin);
                return ms;
            }
        }
    

    .net wpf winrt

    2022-09-30 22:05

    1 Answers

    I had the same problem.
    I don't know why the process remains and zombies, but the way to avoid leaving a process is to disable the standard x button and exit from other buttons or menus to exit without leaving any process.It's not a smart solution, but I think it's a good idea to try it

    <Window:Class=""
            xmlns="...
            Closin="window_closing"<!--add -->...>
    
    private void window_closing(object sender, System.ComponentModel.CancelEventArgse){
        MessageBox.Show("Exit from menu..."); // Any action
        e.Cancel=true;
    }
    

    You can now disable the standard x button.
    Another way to close is to create a batch and run the batch when some event occurs (I have confirmed that there is no process left when I exit the application using Taskkill).

    @echo off
    taskkill/F/IM filename .exe
    
    private void onClickClose(object sender, RoutedEventArgse){
        Process process = new Process();
        process.StartInfo.FileName="close.bat";
        process.StartInfo.CreateNoWindow=true;
        process.StartInfo.UseShellExecute=false;
        process.Start()
    }
    

    This prevents the process from becoming a zombie.


    2022-09-30 22:05

    If you have any answers or tips


    © 2024 OneMinuteCode. All rights reserved.