Solved

C# Windows Service problem

Posted on 2006-06-08
3
220 Views
Last Modified: 2010-04-16
I have a windows service that is behaving weird.  I have log function that when the property file is set to true logs data to the database and false doesnt.  My problem is that when i set it to true, the service runs but if i disable the log it doesnt.  Also, when i debug it in visual studio it runs fine.  Its seems that when i let it run alone without logging is when it fails.  Now in my service i use an async call and not sure if this is what is causing it.  Does a service need time gaps to properly work?

        protected override void OnStart(string[] args)
        {
            string path = null;

            try
            {
                Commons.Logger.LogError(this, "OnStart", "Entry");

                Commons.Logger.Log(this, "OnStart", "Start Thread");
                _MailBuilderThread = new Thread(new ThreadStart(MailBuilderProcess));
                _MailBuilderThread.Start();

                Commons.Constants.MailBuilderProcessRunning = true;

            }
            catch (Exception err)
            {
                _ErrorMessage = err.ToString();
                Commons.Logger.LogError(this, "OnStart", _ErrorMessage);
            }
        }


        public void MailBuilderProcess()
        {
            bool result = false;
            IAsyncResult sync_process = null;
            ExecuteMailBuilderProcessDelegate mail_builder_delegate = null;
            int timer = 0;
           

            try
            {

                Commons.Logger.LogError(this, "MailBuilderProcess", "Entry");

                timer = Commons.Commons.SleepTimer(MailerApplicationServices.Commons.SleepTimerFor.Builder);
                Commons.Logger.Log(this, "MailBuilderProcess", "Timer=" + timer.ToString());

               
                while (Commons.Constants.MailBuilderProcessRunning == true)
                {
                    Commons.Logger.Log(this, "MailBuilderProcess", "Started");
                   
                    mail_builder_delegate = new ExecuteMailBuilderProcessDelegate(ExecuteMailBuilderProcess);

                    Commons.Logger.Log(this, "MailBuilderProcess", "Created delegate");

                    //Commons.Logger.LogError(this, "MailBuilderProcess", "1");
                    sync_process = mail_builder_delegate.BeginInvoke(null, null);

                    Commons.Logger.Log(this, "MailBuilderProcess", "BeginInvoke");
                    //Commons.Logger.LogError(this, "MailBuilderProcess", "2");

                    WaitHandle.WaitAll(new WaitHandle[] { sync_process.AsyncWaitHandle });

                    Commons.Logger.Log(this, "MailBuilderProcess", "All instances finished");

                    //Commons.Logger.LogError(this, "MailBuilderProcess", "3");
                    mail_builder_delegate.EndInvoke(sync_process);
                    //Commons.Logger.LogError(this, "MailBuilderProcess", "4");


                    Commons.Logger.Log(this, "MailBuilderProcess", "Going to sleep");
                    Thread.Sleep(timer);
                    //Commons.Logger.LogError(this, "MailBuilderProcess", "5");
                }
            }
            catch (Exception err)
            {
                _ErrorMessage = err.ToString();
                Commons.Logger.LogError(this, "MailBuilderProcess", _ErrorMessage);
            }

            //return result;
        }
0
Comment
Question by:borg48
3 Comments
 
LVL 21

Accepted Solution

by:
mastoo earned 70 total points
ID: 16870489
How is it failing?  Do you get an unhandled exception or something?

A quick thing to fix would be reorder your startup code:

Commons.Constants.MailBuilderProcessRunning = true;
 _MailBuilderThread = new Thread(new ThreadStart(MailBuilderProcess));
 _MailBuilderThread.Start();

otherwise there's no guarantee it will be true when the thread tests it
0
 
LVL 12

Expert Comment

by:topdog770
ID: 16870982
I think mastoo has hit it correctly.

By leaving the logging in place you've created enough of a 'delay' that the

<< Commons.Constants.MailBuilderProcessRunning = true; >> line has a good chance of being hit before the
thread checks it's value.

0
 

Author Comment

by:borg48
ID: 16872084
That makes so much sense.  The reason i had it on the end was to make sure it started first then set it.  I removed the logging and moved it to the top and it worked.

Thanks all
0

Featured Post

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

778 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question