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 >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;
}
}
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.
© 2024 OneMinuteCode. All rights reserved.