Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8108813
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T01:13:47+00:00 2026-06-06T01:13:47+00:00

My application prints (to a printer) the information shown on screen (using the Canvas

  • 0

My application prints (to a printer) the information shown on screen (using the Canvas control) N times.

The process is

The user clicks a button (called Print).
Update the Canvas with text (normally from a database but for the code below, it’s hard coded)
Print to printer
Update the Canvas with new text (again from a database but for the code below, it’s hard coded)
Print to printer

However, I can’t get this to work as explained in the above process- the printer only prints the last update made.

To make this issue replicable, I enclose the 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 my code behind

using System;
using System.Windows;
using System.Printing;
using System.Windows.Threading;
using System.Windows.Controls;

namespace WpfApplication4
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        PrintDialog dialog = new PrintDialog();
        for (int i = 1; i < 3; i++)
        {
            //showing this message box fixes the issue
            //MessageBox.Show("01");
            updateTextblock(i);

            //use the dispatcher object to ensure all renders and databinding are completed before sending to 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 the printer  
        dialog.PrintQueue = queue;

        dialog.PrintVisual(canvas1, "");
    }

    private void updateTextblock(int i)
    {
        TextBlock1.Text = "Number " + i.ToString();
    }
}
}    

The only thing which prints is
Number 2

Although it has iterated and updated the canvas with Number 1 it never prints (a blank page is printed).

Any ideas what it is I need to do so each Canvas prints? Although I can get it to work by showing the messagebox it defeats the purpose of it being automated.

EDIT: I am now getting an error message from my printer – “Another computer is using the printer.” According to other websites, I have to wait until one job finishes and then the second will start automatically but sadly, it never does.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T01:13:50+00:00Added an answer on June 6, 2026 at 1:13 am

    The answer (sorry the code is not exactly the same as the question but it’s so simple it should be easy to follow. Thank you every one.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
    using System.Windows.Threading;
    
    
    namespace WpfApplication1
    {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
            }
    
            private void Button_Click(object sender, RoutedEventArgs e)
            {
                for (int i = 0; i < 2; i++)
                {
                result res = null;     
                if (i == 0)
                res = new result { Comments = "First Time Round" };
    
                if (i == 1)
                    res = new result { Comments = "Total hard type" };
    
                Dispatcher.Invoke(DispatcherPriority.Loaded, new Action(delegate
                    {   
                        this.DataContext = res;
                    }
                ));
    
                System.Threading.Thread.Sleep(500);
    
                Dispatcher.Invoke(DispatcherPriority.Loaded, new Action(delegate
                {
                    PrintDialog dialog = new PrintDialog();
                    dialog.PrintVisual(this.canvas1, "");
                }
            ));
                System.Threading.Thread.Sleep(500);
    
                }
            }
        }
    
        class result
        {
            public string Comments { get; set; }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I determine if printer is connected? Typically this application prints to the
I've added functionality to an application that prints out a bunch of information to
I'm using fwrite in an MFC application to print content of lBuffer as shown
I have an application that prints text and images to pages on a printer.
I am writing a small application that prints some stickers to a special printer.
I’m writing an application that prints addresses directly onto envelopes. I’m using QTextDocument and
I have an application that prints by generating text files with embedded printer codes,
I am developing a full screen kiosk application using c#. I need to print
I have an application which talks to USB printer and prints out the receipt
I am using wifi printer from my android application to print any file. Now

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.