c# - XPS Printing of Visuals from different thread than the UI thread - is it possible? -


why getting strange exception when i'm trying write xps document?

xpspackagingexception:  package has root documentsequence or fixeddocument. 

my goal print wpf visual xps file, because visual tree big, printing process slow, want find out how can print xps files on background thread, users won't annoyed because of frozen application.

here code:

dispatcher otherthreaddispatcher; public mainwindow() {     initializecomponent();     thread thread = new thread(() =>     {        otherthreaddispatcher =  dispatcher.currentdispatcher;        printit.click += new routedeventhandler(printit_click);        system.windows.threading.dispatcher.run();     });     thread.setapartmentstate(apartmentstate.sta);     thread.start(); }  void printit_click(object sender, routedeventargs e) {     otherthreaddispatcher.invoke(new action(() =>     {        using (xpsdocument mydoc = new xpsdocument("foo.xps", fileaccess.readwrite))        {            xpsdocumentwriter writer = xpsdocument.createxpsdocumentwriter(mydoc);            var collator = writer.createvisualscollator();            collator.beginbatchwrite();            collator.write(someimagetoprint);            collator.endbatchwrite();        }     })); } 

why not use asynchronous pattern async , await or tasks? way don't have bother threading much.


Comments