Avatar of zozig
zozigFlag for United States of America

asked on 

VM CPU monitoring using events

Hi everyone,

I'm trying to create a C# program which monitors the cpu level of vms within virtual server.  I've created an event model to captured when the cpu reaches a certain level.  What I am getting stuck on is the best way for me to actually poll the vs and determine when the cpu has hit a certain level.  tried creating an infinite loop that will constantly pool the cpu level, however I'm never done any kind of thread base programming and my current approach will lock the execution of my program.  Can anyone offer any advice on how I would create a monitoring program which constantly polls.  If I should use different threads how should I create and manage them?  Here is what I have so far and you'lll see why I'm having problems:

 public void monitorCpuUtilization()
       {
           while (true)
           {
               Thread.Sleep(6000);
               

               if (CPUUsageAverage > 10)
               {
                   dixpatchcpuUtilizationEvent();
               }
           }
       }

Al input welcome.  Thanks.

.NET Programming

Avatar of undefined
Last Comment
Gautham Janardhan
Avatar of Gautham Janardhan
Gautham Janardhan

static void Main(string[] args)
            {
                  System.EventHandler FHandler =
                        new EventHandler(monitorCpuUtilization);
                  AsyncCallback Fcallback =
                        new AsyncCallback(CallBack);
                  IAsyncResult FResult = FHandler.BeginInvoke(null,null,Fcallback,null);//asyn chronusly invoke monitorCpuUtilization on another thread
                  //do other stuff
                  //after finishing stuff if u want to wait for monitorCpuUtilization to exit
                  while(!FResult.IsCompleted)
                  {
                        //infinte loop to wait
                  }
            }
            public static void CallBack(IAsyncResult CallBack)
            {
                  // this function is called when monitorCpuUtilization finishes
                  ((System.EventHandler)
                        ((System.Runtime.Remoting.Messaging.AsyncResult)CallBack).AsyncDelegate)
                        .EndInvoke(CallBack);
            }

            public static void monitorCpuUtilization(object sender,EventArgs e)
            {
                  while (true)
                  {
                        Thread.Sleep(6000);
                        if (CPUUsageAverage > 10)
                        {
                              dixpatchcpuUtilizationEvent();
                              break;//to break the loop
                        }
                  }
            }
Avatar of zozig
zozig
Flag of United States of America image

ASKER

Hi gauthampj,

Thanks for the quick response, can you tell me what this would look like within if I am using objects outside of a main method.  For example this code is being launch from a Windows Form and has other objects that handle events.  The following code demonstrates how I would like to handle the events:

            Demo.startDemo(dxDemo.DemoData);
            DXMonitor monEvents = new DXMonitor(); // Custom event handler
            for (int i = 0; i < dxDemo.DemoData.Count; i++)
            {
                Hashtable thisMap = (Hashtable)dxDemo.DemoData[i];

                VirtualMachine vm = (DXVirtualMachine)thisMap["VM"];
                vm.subscribeCPUUtilizationEvent(monEvents.handleCpuUtililzationEvent);  // Subscibe to event
                // Make a sepearate thread call to monitor the cpu event
               // Here is where I need thread help .....
                 vm.monitorCpu()                
           }
        }

Can you show me how I would make an asynchronous call to the monitorCpu() method, also will this be ok to do multiple times as I will have many different objects in the for loop above. Thanks for you all you help.


ASKER CERTIFIED SOLUTION
Avatar of Gautham Janardhan
Gautham Janardhan

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
.NET Programming
.NET Programming

The .NET Framework is not specific to any one programming language; rather, it includes a library of functions that allows developers to rapidly build applications. Several supported languages include C#, VB.NET, C++ or ASP.NET.

137K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo