CPU % Utilization on ASP.Net in Threads

Discussion in 'ASP .Net' started by ipramod@gmail.com, Dec 15, 2007.

  1. Guest

    Hi,

    I just wanted to calculate CPU Percentage Utilization in ASP.Net
    application using a thread.
    I have written following code:

    protected void Page_Load(object sender, EventArgs e)
    {
    btnTestCPUPerc.Click += new
    EventHandler(btnTestCPUPerc_Click);
    }

    // button click event handler
    void btnTestCPUPerc_Click(object sender, EventArgs e)
    {
    // Create thread to calculate CPU Perc Usage
    Thread t = new Thread(new ThreadStart(ThreadProc));
    t.IsBackground = true;
    t.Start();

    // Start inserting database tables

    SqlConnection insertConn = new SqlConnection();
    insertConn.ConnectionString = "Data Source=localhost;Initial
    Catalog=master;Integrated Security=SSPI;";

    insertConn.Open();

    SqlCommand insertComm = new SqlCommand();
    insertComm.CommandText = "INSERT INTO [master]..[TestTable]
    SELECT Guid, Name FROM [temp]..[NewTable]";
    insertComm.Connection = insertConn;
    int i = 0;
    while( i < 10000 )
    {
    insertComm.ExecuteNonQuery();
    i++;
    }
    }

    public static void ThreadProc()
    {
    // Calculate CPU Perc Usage
    PerformanceCounter pc = new PerformanceCounter("Processor", "%
    Processor Time", "_Total");
    float cpuperc = pc.NextValue();

    if (double.Parse(cpuperc.ToString()) > 10)
    {
    // Log event if CPU Perc Usage > 10
    EventLog log = new EventLog("Application");
    log.WriteEntry("CPU Percentage is greater than 10%.
    Current CPU Percentage is: " + double.Parse(cpuperc.ToString()));
    }
    }



    I am inserting rows from one table to other and in the mean time I
    want to raise an event if CPU % goes above 10.

    Please help me out.

    Thanks in adv.

    PI
     
    , Dec 15, 2007
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. mcollier

    w3wp.exe High CPU Utilization

    mcollier, Mar 11, 2005, in forum: ASP .Net
    Replies:
    6
    Views:
    61,309
    jeandemo
    Apr 13, 2011
  2. =?Utf-8?B?UGV0ZXI=?=

    KB # 840861 and 100% CPU utilization

    =?Utf-8?B?UGV0ZXI=?=, Mar 9, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    458
    =?Utf-8?B?UGV0ZXI=?=
    Mar 9, 2006
  3. Avizz
    Replies:
    1
    Views:
    4,462
  4. apondu
    Replies:
    1
    Views:
    5,700
    Rob Conklin
    Apr 27, 2007
  5. Imran
    Replies:
    1
    Views:
    420
    bruce barker
    Oct 6, 2008
Loading...

Share This Page