0

I am trying to make the following code a bit more efficient, it works as it should but it takes 12% of processor resources. I have a feeling this can be done more nicely.

this is how my localService looks like:

public class localService
{
    public string ID { get; set; }

    public string Name { get; set; }

    public string Server { get; set; }

    public string Status { get; set; }
}

This is the function im trying to improve.

        while (running)
        {
            try
            {
                IEnumerable<localService> serviceList = Startup.ServiceList;
                foreach (var service in serviceList)
                {
                    using (var sc = new ServiceController(service.ID))
                    {
                        if (sc.Status.ToString() != service.Status)
                        {
                            // Do some work here
                        }
                    }
                }
            }

            catch (Exception)
            {
                running = false;
            }
        }

the serviceList contains 4 localService objects at the moment. I have written this in a console app with the intention to make it a windows service.

4
  • Do you think it should take more or less than 12% CPU? What does "more efficient" mean in that context? Commented May 27, 2013 at 4:57
  • Any specific reasons other than just the feeling that it should take less than 12% resources Commented May 27, 2013 at 4:58
  • @nvoigt well i think that a fairly simple check for a string shouldn't use 12% cpu. since this will be the only thing this litte app does. V4Vendetta: nope, its just a feeling (and a lack of knowledge) Commented May 27, 2013 at 5:01
  • Have you tried Visual Studios Performance profiler? It could perhaps point you in the direction of your prolem Commented May 27, 2013 at 5:08

1 Answer 1

1

If you don't need it to run as fast as possible, you could add a Thread.Sleep(ms); in at the end of the loop. It causes your program to do nothing for roughly the number of milliseconds you pass in.

Thread.Sleep(250);

would cause the loop to run about 4 times a second if it doesn't take long to execute.

Sign up to request clarification or add additional context in comments.

4 Comments

i see how this can improve but im trying to obtain real-time information if a status changes. However, if i cant find anything better, this will be my fallback. Better near real-time than a slow performing app.
I did some further testing with this. Putting in Thread.Sleep(100); made reduced the cpu usage to 0 - 1%. This works like a charm. Just out of curiosity: why is it, that it makes this kind of improvement?
@Donniewiko Without knowing what you have in the loop, there isn't a lot anyone can suggest. If you don't want to thrash the processor, you can add a sleep. If you want to know how to make your code more efficient, use a profiler to tell you how long each part of the code is executing. If the ServiceController constructor is taking the longest you could precreate and cache them, for example.
@Donniewiko Without the sleep, regardless of how long your code takes, it will run until it hits a limit. If it isn't bound by disk or network I/O, it might run as often as your RAM or processor will allow, consuming a lot of processor time. The sleep just limits the rate.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.