Solved

C# Windows Service problem

Posted on 2006-06-08
3
217 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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

743 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now