CPU % Utilization on ASP.Net in Threads

I

ipramod

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,967
Messages
2,570,148
Members
46,694
Latest member
LetaCadwal

Latest Threads

Top