Tvd
Tvd

Reputation: 4601

An application consuming almost 100% CPU on A SYSTEM

My application uses some webservice's and gets connectd to a server and sits on the system tray. Application just reads from 3rd part app (local) that is used to connect to the server. If it reads any inappropriate line, then fires/handles appropriate events. Thar's it. Had tested on our normal working systems and it is working fine. On my system Quad 2.66GHz and 3GB of RAM the app consumes 25 of CPU.

Recently checked on a PIV 3.0Ghz system containing 2GB of RAM system, the application consumes 98 of CPU.

What can this be ? On a better processor the app consumes almost 4 times more of CPU than mine. Why so ? What can be the reasons for the same. Any ideas to handle out the same.

Once connected, this is the code that is active A cmd is run for using 3rd party app and I use this code to read the output of the same:

    private void Process_OutputDataReceived(object sender, DataReceivedEventArgs e)
    {
        string d = e.Data;

        if (!string.IsNullOrEmpty(d))
        {
            if (sb != null)
                sb.Append(d + "\n");
            //Console.WriteLine("LINE = " + d);
            ERROR_CODE = -1;
            if (d.IndexOf("Initialization Sequence Completed") >= 0)
            {
                connected = true;
                Console.WriteLine("********* Connected = " + connected);
                return;
            } else if (isInValidLine(d))
            {
                if (d.IndexOf("Exiting") >= 0 || d.IndexOf("The requested name is valid but does not have an IP address.") >= 0)
                {
                    if (caughtExp == false)
                    {
                        connected = false;
                        caughtExp = true;
                        errorMsg = d;
                        ERROR_CODE = ERROR_EXIT;
                        OnOpenVpnDisConnectionEvent();
                    }
                }
                else if (d.IndexOf("errno=", StringComparison.OrdinalIgnoreCase) > 0
                    || d.IndexOf("error:2006D080", StringComparison.OrdinalIgnoreCase) >= 0
                    || d.IndexOf("code=995", StringComparison.OrdinalIgnoreCase) >= 0
                    || d.IndexOf("There are no TAP-Win32 adapters on this system", StringComparison.OrdinalIgnoreCase) >= 0
                    || d.IndexOf("Error opening configuration file", StringComparison.OrdinalIgnoreCase) >= 0
                    || (d.IndexOf("CreateIpForwardEntry: Access is denied.", StringComparison.OrdinalIgnoreCase) >= 0 
                                        && d.IndexOf("[status=5 if_index=", StringComparison.OrdinalIgnoreCase) >= 0)
                    || d.IndexOf("CreateFile failed on TAP device") >= 0  )
                {
                    // Want to handle all and go for ReConnect, atmost possible
                    caughtExp = true;
                    connected = false;
                    errorMsg = d;
                    ERROR_CODE = ERROR_FATAL;
                    OnOpenVpnDisConnectionEvent();
                }

                return;
            }
        }
        return;
    }

    private bool isInValidLine(string line)
    {
        if (line.IndexOf("errno=", StringComparison.OrdinalIgnoreCase) >= 0 
            || line.IndexOf("error:2006D080", StringComparison.OrdinalIgnoreCase) >= 0
            || line.IndexOf("code=995", StringComparison.OrdinalIgnoreCase) >= 0
            || line.IndexOf("There are no TAP-Win32 adapters on this system", StringComparison.OrdinalIgnoreCase) >= 0
            || line.IndexOf("Error opening configuration file", StringComparison.OrdinalIgnoreCase) >= 0
            || line.IndexOf("Exiting") >= 0
            || line.IndexOf("The requested name is valid but does not have an IP address.") >= 0
            || line.IndexOf("CreateFile failed on TAP device") >= 0    // && (line.IndexOf("General failure (ERROR_GEN_FAILURE) (errno=31)") >= 0) ) 
            )
        {
            return true;
        }

        return false;
    }

Why does it take so much of CPU on that system ?

Any help is highly appreciated.

Upvotes: 1

Views: 3002

Answers (2)

Adriano Repetti
Adriano Repetti

Reputation: 67070

It consumes all the available CPU in the execution thread: almost 100% for a single core, 25% for a 4 cores CPU (100% of the core where it's executing).

As pointed by @Douglas it may be the result of a loop or a simply your application consumes data more slowly than their production).

If this is its behavior then it'll use all available CPU time. If you do not want to use all CPU time (because it slows down your system or it consumes too much battery) you may decrease process priority. You can't set the priority of the System.Windows.Forms.Timer object (it has the priority of the process). The question is do you really have to call that function so often? When minimized can't you disable it? Anyway I'm thinking: application is consuming 100% of one core only for make-up...I understand it's annoying to change something that works but...

Upvotes: 2

Douglas Leeder
Douglas Leeder

Reputation: 53310

At a guess, the 25% is one complete core, and the P4 (much worse by the way - MHz is a terrible way to compare processors) is single-threaded, so it is completely busy.

You have a busy loop in your code (I suspect), so are keeping one thread of execution CPU-busy all the time.

On a multi-core system other processes will go on to other cores if they are available.

The CPU usage will continue while you have this tight loop, even if it's iterating over an empty collection of connections. Either put a sleep in it, or call the function less frequently, or do the job properly, and do a select or poll so that the OS wakes you when you have data

Upvotes: 3

Related Questions