Solved

C# Windows Service problem

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

680 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