Click here to Skip to main content
15,921,203 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have got a strange problem when calculating network utilization. The code is
C#
bandwidthCounter.CategoryName = "Network Interface";
bandwidthCounter.CounterName = "Current Bandwidth";
bandwidthCounter.InstanceName = networkCard;
bandwidth = bandwidthCounter.NextValue(); // Network card name            

totalDataCounter.CategoryName = "Network Interface";
totalDataCounter.CounterName = "Bytes Total/sec";
totalDataCounter.InstanceName = networkCard; // Network card name
receiveSum = totalDataCounter.NextValue();
            
Console.WriteLine("Bandwidth is " + bandwidth.ToString());
Console.WriteLine("Total bytes is " + receiveSum.ToString());

double utilization = ((8 * receiveSum) / (bandwidth)) * 100;


This code is working perfectly fine when running as a console application. I have changed the console application to a service and started outputing the utilization to a debug window. Strange thing is it works perfectly as a console application matching the utilization with task manager utilization. But when I run the service it shows very different values and does not match with task manager values.
:((

Regards
Surya


More code is here. This is in service application.
C#
namespace NetworkUtilService
{
    public partial class NetworkUtil : ServiceBase
    {
        System.Timers.Timer aTimer;
        public NetworkUtil()
        {
            InitializeComponent();
        }
        protected override void OnStart(string[] args)
        {
            // TODO: Add code here to start your service.
            aTimer = new System.Timers.Timer();
            aTimer.Elapsed += new System.Timers.ElapsedEventHandler(OnTimedEvent);
            aTimer.Interval = 1000;
            aTimer.Enabled = true;  
        }
        protected override void OnStop()
        {
            // TODO: Add code here to perform any tear-down necessary to stop your service.
        }
        public void GetNetWorkUtil()
        {
           
            float sendSum = 0;
            float receiveSum = 0;
            float bandwidth = 0;
            string networkCard = "Broadcom NetXtreme 57xx Gigabit Controller";
            PerformanceCounter bandwidthCounter = new PerformanceCounter();
            bandwidthCounter.CategoryName = "Network Interface";
            bandwidthCounter.CounterName = "Current Bandwidth";
            bandwidthCounter.InstanceName = networkCard;
            bandwidth = bandwidthCounter.NextValue();
            PerformanceCounter totalDataCounter = new PerformanceCounter();
            totalDataCounter.CategoryName = "Network Interface";
            totalDataCounter.CounterName = "Bytes Total/sec";
            totalDataCounter.InstanceName = networkCard;
            receiveSum = totalDataCounter.NextValue();
          
            Debug.OutputDebugString("Total bytes: " + receiveSum.ToString() + " at Time " + DateTime.Now.Second.ToString());
            double utilization = ((8 * receiveSum) / (bandwidth)) * 100;           
            
        }
        
        private void OnTimedEvent(object source, ElapsedEventArgs e)
        {
            GetNetWorkUtil();
        }
    }


And the console application code is
C#
namespace NetworkUtilization
{
    class Calculate
    {
        PerformanceCounter bandwidthCounter = null;
        PerformanceCounter totalDataCounter = null;
        string networkCard = null;
        public Calculate()
        {
            bandwidthCounter = new PerformanceCounter();
            totalDataCounter = new PerformanceCounter();
            networkCard = "Broadcom NetXtreme 57xx Gigabit Controller";
            SetTimer();
            Console.ReadLine();
        }
        public void SetTimer()
        {
            Timer myTimer = new System.Timers.Timer();
            myTimer.Elapsed += new ElapsedEventHandler(myTimer_Elapsed);
            myTimer.Enabled = true;
            myTimer.Interval = 1000;            
        }
        public void myTimer_Elapsed(object sender, ElapsedEventArgs e)
        {           
            float receiveSum = 0;
            float bandwidth = 0;
           
            
            bandwidthCounter.CategoryName = "Network Interface";
            bandwidthCounter.CounterName = "Current Bandwidth";
            bandwidthCounter.InstanceName = networkCard;
            bandwidth = bandwidthCounter.NextValue();
                        
            totalDataCounter.CategoryName = "Network Interface";
            totalDataCounter.CounterName = "Bytes Total/sec";
            totalDataCounter.InstanceName = networkCard;
            receiveSum = totalDataCounter.NextValue();
          
            Console.WriteLine("Bandwidth is " + bandwidth.ToString());
            Console.WriteLine("Total bytes is " + receiveSum.ToString());
            double utilization = ((8 * receiveSum) / (bandwidth)) * 100;
            Console.WriteLine("Utilization is " + utilization.ToString() + " " + DateTime.Now.ToString());
        }
    }


Thanks
Surya
Posted
Updated 20-Jan-11 19:13pm
v4
Comments
Sergey Alexandrovich Kryukov 21-Jan-11 0:29am    
Sounds strange... may be a look at some more of your code could help...
DaveAuld 21-Jan-11 1:14am    
Edit: Added code blocks rounds the last 2 code blocks.

I am not sure about my explanation but I guess this is correct reason.
It takes some amount time to retrieve data from the service.

For example you took data of network at time 11:00:05 time but the service returned the data to the client on 11:00:06, hence there is a mismatch.

You can try the same code to run in a thread with some amount of sleep which will work as interval.
 
Share this answer
 
Hi Debojyoti,

It is not only mismatch, actually it is dropping drastically. When the console aplication shows 9% of utilization, service application shows 1%. How I am unable to understand?

Regards
Surya
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900