By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,638 Members | 1,983 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,638 IT Pros & Developers. It's quick & easy.

Help with creating a windows service

P: n/a
Hi,
I need to create a windows service that will poll a database every 10 sec
and print processed orders to a printer. Where can I find info on this?

Thanks
Nov 22 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
To create Windows service, create a class that inherits from System.ServiceProcess.ServiceBase, System.ServiceProcess

http://msdn.microsoft.com/library/de...classtopic.asp

For info on printing using .NET:

http://msdn.microsoft.com/library/de...asp?frame=true

Here's an example:

Start a timer when the service is started to perform some action every n milliseconds:

public class MyService : System.ServiceProcess.ServiceBase
{
private System.Threading.Timer timer;

/// <summary>Main entry point of the application. (*.exe)</summary>
public static void Main()
{
using (MyService service = new MyService())
{
// Blocks the thread until the serivce is stopped by a service controller. (You can go to services in administrative
tools, select your service, and click Stop)
System.ServiceProcess.ServiceBase.Run(service);
}
}

public MyService()
{
// tell the service control manager what operations can be performed on our service
CanShutdown = CanPauseAndContinue = CanHandlePowerEvent = false;
CanStop = true;
}

public override void OnStart(string[] args)
{
timer = new Timer(new System.Threading.TimerCallback(Poll), null, 0, 10000); // Poll every 10 seconds

// return immediately so the serivce controller knows that your service started successfully.
// You have 30 seconds to return from this method or the serivce will be stopped automatically by the SCM.
}

/// <summary>Called on a thread-pool thread at the specified interval. Do not process any asyncronous calls.</summary>
private void Poll(object state)
{
// do your polling here

if (hasStuffToPrint)
Print(stuffToPrint);
}

/// <summary>Called on a thread-pool thread. Do not process any asyncronous calls.</summary>
private void Print(object stuffToPrint)
{
// do your printing here.
}

protected override void Dispose(bool disposing)
{
if (disposing)
{
if (timer != null)
timer.Dispose();
}
}
}
Good luck :)
--
Dave Sexton
dave@www..jwaonline..com
-----------------------------------------------------------------------
"Chris" <Ch***@discussions.microsoft.com> wrote in message news:DD**********************************@microsof t.com...
Hi,
I need to create a windows service that will poll a database every 10 sec
and print processed orders to a printer. Where can I find info on this?

Thanks

Nov 22 '05 #2

P: n/a
In article <DD**********************************@microsoft.co m>,
Ch***@discussions.microsoft.com says...
Hi,
I need to create a windows service that will poll a database every 10 sec
and print processed orders to a printer. Where can I find info on this?


"Writing a simple service application"
http://tinyurl.com/8mj8

--
Patrick Steele
Microsoft .NET MVP
http://weblogs.asp.net/psteele
Nov 22 '05 #3

P: n/a
Hi,
I want to be able to print the results from the database in an invoice
format, with all the columns and so on. I am not sure where to start from. Is
is possible to render each order in an html format (may be i can design the
invoice layout in html) and then send the print to printer without no dialog
boxes poping up.

"Dave" wrote:
To create Windows service, create a class that inherits from System.ServiceProcess.ServiceBase, System.ServiceProcess

http://msdn.microsoft.com/library/de...classtopic.asp

For info on printing using .NET:

http://msdn.microsoft.com/library/de...asp?frame=true

Here's an example:

Start a timer when the service is started to perform some action every n milliseconds:

public class MyService : System.ServiceProcess.ServiceBase
{
private System.Threading.Timer timer;

/// <summary>Main entry point of the application. (*.exe)</summary>
public static void Main()
{
using (MyService service = new MyService())
{
// Blocks the thread until the serivce is stopped by a service controller. (You can go to services in administrative
tools, select your service, and click Stop)
System.ServiceProcess.ServiceBase.Run(service);
}
}

public MyService()
{
// tell the service control manager what operations can be performed on our service
CanShutdown = CanPauseAndContinue = CanHandlePowerEvent = false;
CanStop = true;
}

public override void OnStart(string[] args)
{
timer = new Timer(new System.Threading.TimerCallback(Poll), null, 0, 10000); // Poll every 10 seconds

// return immediately so the serivce controller knows that your service started successfully.
// You have 30 seconds to return from this method or the serivce will be stopped automatically by the SCM.
}

/// <summary>Called on a thread-pool thread at the specified interval. Do not process any asyncronous calls.</summary>
private void Poll(object state)
{
// do your polling here

if (hasStuffToPrint)
Print(stuffToPrint);
}

/// <summary>Called on a thread-pool thread. Do not process any asyncronous calls.</summary>
private void Print(object stuffToPrint)
{
// do your printing here.
}

protected override void Dispose(bool disposing)
{
if (disposing)
{
if (timer != null)
timer.Dispose();
}
}
}
Good luck :)
--
Dave Sexton
dave@www..jwaonline..com
-----------------------------------------------------------------------
"Chris" <Ch***@discussions.microsoft.com> wrote in message news:DD**********************************@microsof t.com...
Hi,
I need to create a windows service that will poll a database every 10 sec
and print processed orders to a printer. Where can I find info on this?

Thanks


Nov 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.