C# printing with WPF -


my application prints (to printer) information shown on screen (using canvas control) n times.

the process is

the user clicks button (called print).
update canvas text (normally database code below, it's hard coded)
print printer
update canvas new text (again database code below, it's hard coded) print printer

however, can't work explained in above process- printer prints last update made.

to make issue replicable, enclose code below

my xaml

<window x:class="wpfapplication4.mainwindow"     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"     title="mainwindow" height="350" width="525"> <grid>     <canvas margin="0,0,0,88" name="canvas1">         <textblock text="hello world" name="textblock1" />     </canvas>     <button content="button" height="23" horizontalalignment="left" margin="0,245,0,0" name="button1" verticalalignment="top" width="75" click="button1_click" /> </grid> </window> 

and code behind

using system; using system.windows; using system.printing; using system.windows.threading; using system.windows.controls;  namespace wpfapplication4 { /// <summary> /// interaction logic mainwindow.xaml /// </summary> public partial class mainwindow : window {     public mainwindow()     {         initializecomponent();     }      private void button1_click(object sender, routedeventargs e)     {         printdialog dialog = new printdialog();         (int = 1; < 3; i++)         {             //showing message box fixes issue             //messagebox.show("01");             updatetextblock(i);              //use dispatcher object ensure renders , databinding completed before sending print                dispatcheroperation diso;             diso = dispatcher.begininvoke(dispatcherpriority.loaded, new action(delegate             {                 print(dialog);             }             ));                diso.wait()         }     }      private void print(printdialog dialog)     {         //select printer auotmatically         printqueue queue = new localprintserver().getprintqueue("canon mg160 series ws");         //assign printer           dialog.printqueue = queue;          dialog.printvisual(canvas1, "");     }      private void updatetextblock(int i)     {         textblock1.text = "number " + i.tostring();     } } }     

the thing prints number 2

although has iterated , updated canvas number 1 never prints (a blank page printed).

any ideas need each canvas prints? although can work showing messagebox defeats purpose of being automated.

edit: getting error message printer - "another computer using printer." according other websites, have wait until 1 job finishes , second start automatically sadly, never does.

use printdialog dialog = new printdialog(); inside loop use function

 private void button1_click(object sender, routedeventargs e)     {          (int = 1; < 3; i++)         {            printdialog dialog = new printdialog();             //showing message box fixes issue             //messagebox.show("01");             updatetextblock(i);              //use dispatcher object ensure renders , databinding completed before sending print                dispatcher.begininvoke(dispatcherpriority.loaded, new action(delegate             {                 print(dialog);             }             ));            }     } 

Comments

Popular posts from this blog

jquery - Invalid Assignment Left-Hand Side -

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -