Hoping to get UWP print functionality in my Unity UWP app by following the steps in stackoverflow question 9074684 - how-to-print-in-uwp-app Here's the relevant parts of my app: Code (CSharp): #if (ENABLE_IL2CPP && UNITY_WSA_10_0) void InitializePrinting() { printMan = PrintManager.GetForCurrentView(); printMan.PrintTaskRequested += PrintTaskRequested; printDoc = new PrintDocument(); printDocSource = printDoc.DocumentSource; ...... void OnPrint() { UnityEngine.WSA.Application.InvokeOnUIThread(async () => { InitializePrinting(); }, true); } However program is hanging when new PrintDocument() is called... Any ideas, anyone?
So, offhand I noticed your calling InvokeOnUIThread() with true for waitUntilDone parameter; this should be false. This is blocking the calling thread and is probably causing the deadlock
Thanks for responding! I did change the parameter to false, but still seems to hang when creating the PrintDoc object. Also I'm calling the InitializePrinting() function from an OnEnable Code (CSharp): #if (ENABLE_IL2CPP && UNITY_WSA_10_0) void InitializePrinting() { printMan = PrintManager.GetForCurrentView(); printMan.PrintTaskRequested += PrintTaskRequested; printDoc = new PrintDocument(); printDocSource = printDoc.DocumentSource; ...... void OnEnable() { UnityEngine.WSA.Application.InvokeOnUIThread(async () => { InitializePrinting(); }, false); }
I quickly tried this code out but wasn't able to repro the deadlock. So a couple additional questions: - Are you building a "XAML Project" for UWP? Since PrintDocument is a XAML control, you'll need to use the project type. I tried it with D3D and just got a RPC_E_WRONG_THREAD error (but didn't deadlock). - What version of Unity are you using? - Are you using IL2CPP or .NET back end (if on a 217.4 or older release)?
sorry for the delay in responding.. I switched from a D3D to a XAML project using Unity 2018.3.13f1 with IL2CPP backend When printDoc = new PrintDocument(); is called, I'm getting an exception: Exception: Exception of type 'System.Exception' was thrown. at Windows.UI.Xaml.Printing.PrintDocument..ctor () [0x00000] in <00000000000000000000000000000000>:0 at ScreenShotPopUp.InitializePrinting2 () [0x00000] in <00000000000000000000000000000000>:0 at ScreenShotPopUp+<DoScreenshot>d__65.MoveNext () [0x00000] in <00000000000000000000000000000000>:0 at UnityEngine.SetupCoroutine.InvokeMoveNext (System.Collections.IEnumerator enumerator, System.IntPtr returnValueAddress) [0x00000] in <00000000000000000000000000000000>:0 #if (ENABLE_IL2CPP && UNITY_WSA_10_0) void InitializePrinting() { // Printing LogManager.mInstance.Log("ScreenShotPopuUp.InitializePrinting"); LogManager.mInstance.Log("ScreenShotPopuUp.InitializePrinting getting printMan"); printMan = PrintManager.GetForCurrentView(); LogManager.mInstance.Log("ScreenShotPopuUp.InitializePrinting adding PrintTaskRequested"); printMan.PrintTaskRequested += PrintTaskRequested; LogManager.mInstance.Log("ScreenShotPopuUp.InitializePrinting Done"); InitializePrinting2(); } void InitializePrinting2() { // Build a PrintDocument and register for callbacks LogManager.mInstance.Log("ScreenShotPopuUp.InitializePrinting2 creating printdocument"); printDoc = new PrintDocument();
Are you calling InitializePrinting() on the UI thread? From this recent code you pasted it doesn't seem like you are, whereas in your original post you did. Code (CSharp): void Start() { #if (ENABLE_IL2CPP && UNITY_WSA_10_0) UnityEngine.WSA.Application.InvokeOnUIThread(async () => { InitializePrinting(); }, false); #endif I tried the Printing APIs in a 2018.3 release of Unity using IL2PP (invoking them on the UI thread) and didn't hit the exception or run into other issues creating a new PrintDocument.
Looks like I got it working. I think I was attempting to create printDoc a second time. Really appreciate your help.