Windows Service not firing PrintDocument.PrintPage event

G

Guest

There is no microsoft.public.dotnet.framework.windowsservice group, so I am
posting this question here.

Can an object of class PrintDocument work properly in a windows service? The
VS.NET2003 designer allowed me to drop a PrintDocument control onto the
design page of my windows service. I am using version 1.1 of the framework.

Problem: The eventLog1.WriteEntry("inside PrintPage event handler") is never
executed, while the other WriteEntry messages appear correctly in my event
log.

Here is the code that I added to my service:

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Printing;
using System.ServiceProcess;

namespace MyPrintService
{
public class MyPrintService : System.ServiceProcess.ServiceBase
{
private System.Diagnostics.EventLog eventLog1;
private System.Timers.Timer timer1;
private System.Drawing.Printing.PrintDocument printDocument1;
private System.Drawing.Font printFont;

....

private void InitializeComponent()
{
this.eventLog1 = new System.Diagnostics.EventLog();
this.timer1 = new System.Timers.Timer();
this.printDocument1 = new System.Drawing.Printing.PrintDocument();
this.printFont = new System.Drawing.Font("Arial", 10);

((System.ComponentModel.ISupportInitialize)(this.eventLog1)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.timer1)).BeginInit();
//
// timer1
//
this.timer1.Enabled = true;
this.timer1.Interval = 10000;
this.timer1.Elapsed += new
System.Timers.ElapsedEventHandler(this.timer1_Elapsed);
//
// printDocument1
//
this.printDocument1.PrintPage += new
System.Drawing.Printing.PrintPageEventHandler(this.printDocument1_PrintPage);
//
// MyPrintService
//
this.ServiceName = "MyPrintService";
((System.ComponentModel.ISupportInitialize)(this.eventLog1)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.timer1)).EndInit();

}

private void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
int id = FindOrderToPrint();
eventLog1.WriteEntry("timer elapsed with id = " + id.ToString());
if (id > 0)
{
eventLog1.WriteEntry("call to PrintOrder");
PrintOrder(id);
}
}

private int FindOrderToPrint()
{
int OrderID;
try
{
// sqlConnection1.Open();
// SqlCommand cmdGetOrderID = new SqlCommand(...);
// OrderID = (int)cmdGetOrderID.ExecuteScalar();
OrderID = 10;
eventLog1.WriteEntry("Order ID " + OrderID.ToString() + " sent to
cashier");
sqlConnection1.Close();
}
catch
{
OrderID = -1;
eventLog1.WriteEntry("no file to print");
}
return OrderID;
}

private void PrintOrder(int OrderID)
{
eventLog1.WriteEntry("print OrderID = " + OrderID.ToString());
this.printDocument1.Print();
}

private void printDocument1_PrintPage(object sender,
System.Drawing.Printing.PrintPageEventArgs e)
{
eventLog1.WriteEntry("inside PrintPage event handler");
e.Graphics.DrawString("Test string to print", this.printFont,
System.Drawing.Brushes.Black, 50, 50);
e.HasMorePages = false;
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top