[Please note: this issue was effectively resolved in my other question: How to use the undo cancel function to cancel background printing ]
I used the following method (from SO) to start my printers and preview in the background thread:
public static Task StartSTATask(Action func) { var tcs = new TaskCompletionSource<object>(); var thread = new Thread(() => { try { func(); tcs.SetResult(null); } catch (Exception e) { tcs.SetException(e); } }); thread.SetApartmentState(ApartmentState.STA); thread.Priority = ThreadPriority.AboveNormal; thread.Start(); return tcs.Task; }
It works perfectly, without errors.
I do not understand how to cancel this task correctly. Should I completely stop the flow or pass the CancellationTokenSource token? How has the above code changed to allow cancellation (or abort)?
I am very confused. Thanks for any help with this.
(After long searches, the solution here is not at all as trivial as I hoped!)
After I thought, I tend to pass the CancellationToken to the func () executable and force the function to stop working. In this case, this means that PrintDialogue () is closing. Is there another way?
I use the above code as:
public override Task PrintPreviewAsync() { return StartSTATask(() => {
Hope this helps to better explain my problem.
source share