Click here to Skip to main content
15,899,475 members
Home / Discussions / C#
   

C#

 
AnswerRe: Recursive Permutation Generator. Pin
Luc Pattyn25-Feb-11 16:58
sitebuilderLuc Pattyn25-Feb-11 16:58 
GeneralRe: Recursive Permutation Generator. Pin
shivamkalra25-Feb-11 17:22
shivamkalra25-Feb-11 17:22 
GeneralRe: Recursive Permutation Generator. Pin
Luc Pattyn25-Feb-11 17:32
sitebuilderLuc Pattyn25-Feb-11 17:32 
GeneralRe: Recursive Permutation Generator. Pin
shivamkalra25-Feb-11 17:53
shivamkalra25-Feb-11 17:53 
AnswerRe: Recursive Permutation Generator. Pin
Luc Pattyn25-Feb-11 17:58
sitebuilderLuc Pattyn25-Feb-11 17:58 
GeneralRe: Recursive Permutation Generator. Pin
shivamkalra25-Feb-11 18:20
shivamkalra25-Feb-11 18:20 
AnswerRe: Recursive Permutation Generator. [modified] Pin
Luc Pattyn25-Feb-11 17:14
sitebuilderLuc Pattyn25-Feb-11 17:14 
QuestionHelp with diagnosing a service failing [modified] Pin
turbosupramk325-Feb-11 11:15
turbosupramk325-Feb-11 11:15 
I have written a service and it will start and run, but it terminates itself at variable times.

In the event viewer it only says that the service has terminated unexpectedly. Is there a best practice way to log this or capture this failure? I can't seem to figure out what is causing this failure, it does not fail when I launch the exe in debug mode.

Thanks for reading, here is my main thread code if that will help.



using System;
using System.Diagnostics;
using System.ServiceProcess;
using System.Threading; // go to add reference and select "System.Windows.Forms" under the .net tab
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.DirectoryServices; // go to add reference and select "System.DirectoryServices" under the .net tab
using System.DirectoryServices.ActiveDirectory;
using System.Net;
using System.Collections;
using Microsoft.Win32;






namespace WindowsService
{

    class WindowsService : ServiceBase
    {
        /// <summary>

        /// Public Constructor for WindowsService.

        /// - Put all of your Initialization code here.

        /// </summary>
        /// 


        public WindowsService()
        {
            this.ServiceName = "Emailer";
            this.EventLog.Log = "Emailer Application";

            // These Flags set whether or not to handle that specific

            //  type of event. Set to true if you need it, false otherwise.

            this.CanHandlePowerEvent = true;
            this.CanHandleSessionChangeEvent = true;
            this.CanPauseAndContinue = true;
            this.CanShutdown = true;
            this.CanStop = true;
           

        }


        /// <summary>

        /// The Main Thread: This is where your Service is Run.

        /// </summary>

        static void Main()
        {
            ServiceBase.Run(new WindowsService());
            Thread workerThread = new Thread(new ThreadStart(workThread));
            workerThread.Start();
            //System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;
            string logDate = dataLogging.Logging.returnDate(false, true);
            //dataLogging.Logging.log(@"c:\program files\AllinOneService\logs\" + logDate + "_log.txt", "Service Started - test" + System.Environment.NewLine, true, true);
        }


        static void workThread()
        {
            while (true)
            {
                try
                {
                    RegistryKey scheduleEmailer = Registry.LocalMachine;
                    RegistryKey emailerKey = scheduleEmailer.OpenSubKey(@"SOFTWARE\s\ScheduleEmailer");
                    string dayOfWeek = emailerKey.GetValue("dayOfWeek").ToString();
                    string hourInArmyTime = emailerKey.GetValue("hourInArmyTime").ToString();
                    string minuteFloor = emailerKey.GetValue("minuteFloor").ToString();
                    string minuteCeiling = emailerKey.GetValue("minuteCeiling").ToString();
                    //MessageBox.Show("Registry values are " + dayOfWeek + " " + hourInArmyTime + " " + minuteFloor + " " + minuteCeiling);
                    string isRunning = "false";
                    if ((DateTime.Now.DayOfWeek.ToString() == dayOfWeek) && (DateTime.Now.Hour == Convert.ToInt32(hourInArmyTime))  /*&& (DateTime.Now.Minute.ToString() == "45")*/ && (isRunning == "false"))
                    {
                        if ((DateTime.Now.Minute > Convert.ToInt32(minuteFloor)) && (DateTime.Now.Minute < Convert.ToInt32(minuteCeiling)) /*|| (DateTime.Now.Minute.ToString() == "10") || (DateTime.Now.Minute.ToString() == "15") || (DateTime.Now.Minute.ToString() == "20") || (DateTime.Now.Minute.ToString() == "25") || (DateTime.Now.Minute.ToString() == "30") || (DateTime.Now.Minute.ToString() == "35") || (DateTime.Now.Minute.ToString() == "40") || (DateTime.Now.Minute.ToString() == "45") || (DateTime.Now.Minute.ToString() == "50") || (DateTime.Now.Minute.ToString() == "55")*/)
                        {
                            //MessageBox.Show("Email thread running");
                            isRunning = "true";
                            htmParser.ScheduleParser.emailer();
                            string date = dataLogging.Logging.returnDate(false, true);
                            dataLogging.Logging.log(@"c:\program files\AllinOneService\logs\" + date + "_emaillog.txt", "*** Scheduled email has been sent out at minute " + DateTime.Now.Minute.ToString() + " . ***" + " ... Sleeping for 300 seconds" + System.Environment.NewLine, true);
                            //Thread scheduleEmailer = new Thread(new ThreadStart(scheduleEmail));
                            //scheduleEmailer.Start();
                            Thread.Sleep(300000);
                            isRunning = "false";
                        }
                    }
                    string date2 = dataLogging.Logging.returnDate(false, true);
                    dataLogging.Logging.log(@"c:\program files\AllinOneService\logs\" + date2 + "_emaillog.txt", "Email wait loop occurring at minute " + DateTime.Now.Minute.ToString() + " . Registry is set to " + dayOfWeek + " " + hourInArmyTime + " " + minuteFloor + " to " + minuteCeiling + ". Comparing to " + DateTime.Now.DayOfWeek + " " + DateTime.Now.Hour + " " + DateTime.Now.Minute +  " ... Sleeping for 60 seconds" + System.Environment.NewLine, true);
                    Thread.Sleep(60000);

                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    throw;
                }
            }
        }


 



        /// <summary>

        /// Dispose of objects that need it here.

        /// </summary>

        /// <param name="disposing">Whether

        ///    or not disposing is going on.</param>

        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);
        }

        /// <summary>

        /// OnStart(): Put startup code here

        ///  - Start threads, get inital data, etc.

        /// </summary>

        /// <param name="args"></param>

        protected override void OnStart(string[] args)
        {
            base.OnStart(args);
            Main();
        }

        /// - Stop threads, set final data, etc.

        protected override void OnStop()
        {
            MessageBox.Show("Service is stopped");
            base.OnStop();
        }

        /// - Pause working threads, etc.

        protected override void OnPause()
        {
            base.OnPause();
        }

        /// OnContinue(): Put your continue code here - Un-pause working threads, etc.

        protected override void OnContinue()
        {
            base.OnContinue();
        }

        /// OnShutdown(): Called when the System is shutting down - Put code here when you need special handling of code that deals with a system shutdown, such

        ///   as saving special data before shutdown.

        protected override void OnShutdown()
        {
            MessageBox.Show("Service is shutdown");
            base.OnShutdown();
        }

        /// OnCustomCommand(): If you need to send a command to your service without the need for Remoting or Sockets, usethis method to do custom methods.

        protected override void OnCustomCommand(int command)
        {
            //  A custom command can be sent to a service by using this method:

            //#  int command = 128; //Some Arbitrary number between 128 & 256

            //#  ServiceController sc = new ServiceController("NameOfService");

            //#  sc.ExecuteCommand(command);

            base.OnCustomCommand(command);
        }

        /// <summary>

        /// OnPowerEvent(): Useful for detecting power status changes,

        ///   such as going into Suspend mode or Low Battery for laptops.

        /// </summary>

        /// <param name="powerStatus">The Power Broadcast Status

        /// (BatteryLow, Suspend, etc.)</param>

        protected override bool OnPowerEvent(PowerBroadcastStatus powerStatus)
        {
            return base.OnPowerEvent(powerStatus);
        }

        /// <summary>

        /// OnSessionChange(): To handle a change event from a Terminal Server session.

        ///   Useful if you need to determine when a user logs in remotely or logs off, or when someone logs into the console.


        protected override void OnSessionChange(SessionChangeDescription changeDescription)
        {
            base.OnSessionChange(changeDescription);
        }

        public static void scheduleEmail()
        {
            htmParser.ScheduleParser.emailer();

        }



    }
}



/*

Batch file installer code



:: Installer
@ECHO OFF

REM The following directory is for .NET 2.0
set DOTNETFX2=%SystemRoot%\Microsoft.NET\Framework\v2.0.50727
set PATH=%PATH%;%DOTNETFX2%

echo Installing WindowsService...
echo ---------------------------------------------------
InstallUtil /i "c:\users\Service.exe"
echo ---------------------------------------------------
echo Done.
pause







Batch file uninstaller code



:: Uninstaller
@ECHO OFF

REM The following directory is for .NET 2.0
set DOTNETFX2=%SystemRoot%\Microsoft.NET\Framework\v2.0.50727
set PATH=%PATH%;%DOTNETFX2%

echo Installing WindowsService...
echo ---------------------------------------------------
InstallUtil /u "c:\users\Service.exe"
echo ---------------------------------------------------
echo Done.
pause


*/


modified on Friday, February 25, 2011 6:32 PM

AnswerRe: Help with diagnosing a service failing Pin
RobCroll25-Feb-11 12:22
RobCroll25-Feb-11 12:22 
GeneralRe: Help with diagnosing a service failing Pin
turbosupramk325-Feb-11 12:37
turbosupramk325-Feb-11 12:37 
GeneralRe: Help with diagnosing a service failing Pin
RobCroll25-Feb-11 13:12
RobCroll25-Feb-11 13:12 
GeneralRe: Help with diagnosing a service failing Pin
turbosupramk325-Feb-11 18:06
turbosupramk325-Feb-11 18:06 
GeneralRe: Help with diagnosing a service failing Pin
turbosupramk328-Feb-11 5:30
turbosupramk328-Feb-11 5:30 
GeneralRe: Help with diagnosing a service failing Pin
turbosupramk328-Feb-11 9:12
turbosupramk328-Feb-11 9:12 
GeneralRe: Help with diagnosing a service failing Pin
RobCroll28-Feb-11 11:33
RobCroll28-Feb-11 11:33 
GeneralRe: Help with diagnosing a service failing Pin
RobCroll28-Feb-11 11:29
RobCroll28-Feb-11 11:29 
GeneralRe: Help with diagnosing a service failing Pin
turbosupramk328-Feb-11 15:12
turbosupramk328-Feb-11 15:12 
Questiondraw Pin
om_metab25-Feb-11 10:56
om_metab25-Feb-11 10:56 
Questiondraw Pin
om_metab25-Feb-11 10:42
om_metab25-Feb-11 10:42 
AnswerRe: draw Pin
fjdiewornncalwe25-Feb-11 10:55
professionalfjdiewornncalwe25-Feb-11 10:55 
AnswerRe: draw Pin
OriginalGriff25-Feb-11 21:05
mveOriginalGriff25-Feb-11 21:05 
QuestionMouse cursor modification by getting data from web browser object Pin
HalliHaida25-Feb-11 1:38
HalliHaida25-Feb-11 1:38 
AnswerRe: Mouse cursor modification by getting data from web browser object Pin
Piccadilly Yum Yum25-Feb-11 3:10
Piccadilly Yum Yum25-Feb-11 3:10 
GeneralRe: Mouse cursor modification by getting data from web browser object Pin
HalliHaida27-Feb-11 17:16
HalliHaida27-Feb-11 17:16 
AnswerRe: Mouse cursor modification by getting data from web browser object Pin
Espen Harlinn25-Feb-11 4:13
professionalEspen Harlinn25-Feb-11 4:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.