Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [Conditional("DEBUG_SERVICE")]
- private static void DebugMode()
- {
- Debugger.Break();
- }
- public override void OnStart()
- {
- DebugMode();
- /* ... do the rest */
- }
- public static int Main(string[] args)
- {
- if (!Environment.UserInteractive)
- {
- // Startup as service.
- }
- else
- {
- // Startup as application
- }
- }
- /ConsoleRunner
- /....
- /ServiceRunner
- /....
- /ApplicationLogic
- /....
- using System;
- using System.Threading;
- using System.Configuration;
- public class ServiceEventHandler
- {
- Timer _timer;
- public ServiceEventHandler()
- {
- // get configuration etc.
- _timer = new Timer(
- new TimerCallback(EventTimerCallback)
- , null
- , Timeout.Infinite
- , Timeout.Infinite);
- }
- private void EventTimerCallback(object state)
- {
- // do something
- }
- public void StartEventLoop()
- {
- // wait a minute, then run every 30 minutes
- _timer.Change(TimeSpan.Parse("00:01:00"), TimeSpan.Parse("00:30:00");
- }
- }
- #if DEBUG
- if (!System.Diagnostics.Debugger.IsAttached)
- {
- System.Diagnostics.Debugger.Break();
- }
- #endif
- static void Main()
- {
- #if DEBUG
- // Run as interactive exe in debug mode to allow easy
- // debugging.
- var service = new MyService();
- service.OnStart(null);
- // Sleep the main thread indefinitely while the service code
- // runs in .OnStart
- Thread.Sleep(Timeout.Infinite);
- #else
- // Run normally as service in release mode.
- ServiceBase[] ServicesToRun;
- ServicesToRun = new ServiceBase[]{ new MyService() };
- ServiceBase.Run(ServicesToRun);
- #endif
- }
- static void Main(string[] args) {
- if (Console.In != StreamReader.Null) {
- if (args.Length > 0 && args[0] == "/console") {
- // Start your service work.
- }
- }
- }
- protected override void OnStart(string[] args)
- {
- if (args.Contains<string>("DEBUG_SERVICE"))
- {
- Debugger.Break();
- }
- ...
- }
- protected override void OnStart(string[] args)
- {
- if (args.Contains<string>("DEBUG_SERVICE"))
- {
- Debugger.Break();
- }
- ...
- }
- protected override void OnStart(string[] args)
- {
- if (args.Contains<string>("DEBUG_SERVICE"))
- {
- Debugger.Break();
- }
- ...
- }
- #if DEBUG
- System.Diagnostics.Debugger.Break();
- #endif
- /// <summary>
- /// The main entry point for the application.
- /// </summary>
- static void Main()
- {
- ServiceBase[] ServicesToRun;
- ServicesToRun = new ServiceBase[]
- {
- new MyService(),
- new MyServiceDebug()
- };
- ServiceBase.Run(ServicesToRun);
- }
Add Comment
Please, Sign In to add comment