Wednesday, March 19, 2014

How to use Full CPU Usage using Asp.net C#

Step1: - Create one sample web page 

Step2:- Go to default.aspx page

Step3:- In the default.aspx.cs page add this namespace " using System.Threading; "

Step4:- Before page load method add this line " static AutoResetEvent _AREvt; "

Step5:- In the page load method add this code part

_AREvt = new AutoResetEvent(false);

ThreadStart ts = new ThreadStart(Test);
Thread th = new Thread(ts);
th.IsBackground = true;
th.Priority = ThreadPriority.Lowest;
th.Start();

ts = new ThreadStart(Test1);
th = new Thread(ts);
th.IsBackground = true;
th.Priority = ThreadPriority.Lowest;
th.Start();

ts = new ThreadStart(Test1);
th = new Thread(ts);
th.IsBackground = true;
th.Priority = ThreadPriority.Lowest;
th.Start();

ts = new ThreadStart(Test1);
th = new Thread(ts);
th.IsBackground = true;
th.Priority = ThreadPriority.Lowest;
th.Start();


All this code is working based on the threading concept

Step6:- Add these two methods

static void Test()
        {

            while (true)
            {

                //do what you need todo
                _AREvt.WaitOne(10, true);
                for (Int64 i = 0; i < 10000000000; i++)
                {
                    for (Int64 j = 0; j < 10000000000; j++)
                    {
                        for (Int64 k = 0; k < 10000000000; k++)
                        {

                        }
                    }
                }
            }
        }




static void Test1()
        {
            while (true)
            {

                //do what you need todo
                _AREvt.WaitOne(10, true);
                for (Int64 i = 0; i < 10000000000; i++)
                {
                    for (Int64 j = 0; j < 10000000000; j++)
                    {
                        for (Int64 k = 0; k < 10000000000; k++)
                        {

                        }
                    }
                }

            }
        }

Step7:- Run the project and check the CPU usage

Full page is look like below


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Threading;

namespace SampleTestingCodes
{
    public partial class WebForm2 : System.Web.UI.Page
    {
        static AutoResetEvent _AREvt;
        protected void Page_Load(object sender, EventArgs e)
        {
            _AREvt = new AutoResetEvent(false);

            ThreadStart ts = new ThreadStart(Test);
            Thread th = new Thread(ts);
            th.IsBackground = true;
            th.Priority = ThreadPriority.Lowest;
            th.Start();

            ts = new ThreadStart(Test1);
            th = new Thread(ts);
            th.IsBackground = true;
            th.Priority = ThreadPriority.Lowest;
            th.Start();

            ts = new ThreadStart(Test1);
            th = new Thread(ts);
            th.IsBackground = true;
            th.Priority = ThreadPriority.Lowest;
            th.Start();

            ts = new ThreadStart(Test1);
            th = new Thread(ts);
            th.IsBackground = true;
            th.Priority = ThreadPriority.Lowest;
            th.Start();
        }


        static void Test()
        {

            while (true)
            {

                //do what you need todo
                _AREvt.WaitOne(10, true);
                for (Int64 i = 0; i < 10000000000; i++)
                {
                    for (Int64 j = 0; j < 10000000000; j++)
                    {
                        for (Int64 k = 0; k < 10000000000; k++)
                        {

                        }
                    }
                }
            }
        }

        static void Test1()
        {
            while (true)
            {

                //do what you need todo
                _AREvt.WaitOne(10, true);
                for (Int64 i = 0; i < 10000000000; i++)
                {
                    for (Int64 j = 0; j < 10000000000; j++)
                    {
                        for (Int64 k = 0; k < 10000000000; k++)
                        {

                        }
                    }
                }

            }
        }
    }
}


Demo





For any clarifications or queries please don’t hesitate to call or Email

Phone :- + 91-9492179390


Email :- lannam@technobrainltd.com (or) lokeshtec@gmail.com 

No comments:

Post a Comment