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
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
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)?
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+d__65.MoveNext () [0x00000] in <00000000000000000000000000000000>:0
at UnityEngine.SetupCoroutine.InvokeMoveNext (System.Collections.IEnumerator enumerator, System.IntPtr returnValueAddress) [0x00000] in <00000000000000000000000000000000>:0
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.
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.