Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

C# Windows Service problem

Posted on 2006-06-08
3
Medium Priority
?
226 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 21

Accepted Solution

by:
mastoo earned 280 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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

604 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