Help with creating a windows service

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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
 
To create Windows service, create a class that inherits from System.ServiceProcess.ServiceBase, System.ServiceProcess

http://msdn.microsoft.com/library/d...systemserviceprocessservicebaseclasstopic.asp

For info on printing using .NET:

http://msdn.microsoft.com/library/d...ntingprintcontrollerclasstopic.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 :)
 
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 said:
To create Windows service, create a class that inherits from System.ServiceProcess.ServiceBase, System.ServiceProcess

http://msdn.microsoft.com/library/d...systemserviceprocessservicebaseclasstopic.asp

For info on printing using .NET:

http://msdn.microsoft.com/library/d...ntingprintcontrollerclasstopic.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
[email protected]
-----------------------------------------------------------------------
Chris said:
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
 
Back
Top