Solved

C# Windows Service problem

Posted on 2006-06-08
3
221 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Device Location 4 35
Sent email using Shared Mailbox from outlook in C# 5 50
FTP file download using c# 3 25
bound data table problem 2 33
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
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…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

839 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