微思考源码weisico.com|分享互联网优质资源,免费织梦模板,WP主题下载

ASP.NET微软图表控件MsChart实例之Cpu信息和内存使用监控图表

ASP.NET微软图表控件MsChart实例之Cpu信息和内存使用监控图表

chart_bb682f75a4a24f03a21b8ff59ea8ffdf_15.png

实例使用到了Ajax的方法,Windows Api获取系统内存的方法

新建cpuchart.aspx页面,代码如下

<asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
            <asp:Chart ID="ChartMemory" runat="server" BackColor="LightSteelBlue"
        BackGradientStyle="TopBottom" BackSecondaryColor="White" EnableTheming="False"
        EnableViewState="True" Height="363px" Width="415px">
        <Legends>
        <asp:Legend Alignment="Center" Docking="Bottom" Name="Legend1" Title="图例">
        </asp:Legend>
        </Legends>
        <Titles>
        <asp:Title Font="微软雅黑, 16pt" Name="Title1" Text="系统内存监控图表">
        </asp:Title>
        </Titles>
        <Series>
        <asp:Series BorderColor="White" BorderWidth="3" ChartArea="ChartArea1"
        ChartType="Spline" Legend="Legend1" Name="已使用物理内存" XValueType="Double"
        YValueType="Double">
        </asp:Series>
        <asp:Series BorderWidth="3" ChartArea="ChartArea1" ChartType="Spline"
        Legend="Legend1" Name="全部占用内存">
        </asp:Series>
        <asp:Series ChartArea="ChartArea2" ChartType="StackedArea" Legend="Legend1"
        Name="CPU">
        </asp:Series>
        </Series>
        <ChartAreas>
        <asp:ChartArea BackColor="224, 224, 224" BackGradientStyle="LeftRight"
        Name="ChartArea1">
        </asp:ChartArea>
        <asp:ChartArea Name="ChartArea2">
        </asp:ChartArea>
        </ChartAreas>
        </asp:Chart>
  <asp:Timer ID="Timer1" runat="server" Interval="1000" 
            ontick="Timer1_Tick">
        </asp:Timer>
        </ContentTemplate>
   </asp:UpdatePanel>

添加了图表控件和一个Ajax的计时器以及Ajax的ScriptManager,UpdatePanel,把计时器和图表控件都拖进UpdatePanel里面。注意,在这里需要添加<ContentTemplate>标签,否则报错。将计时器属性的间隔时间为一秒钟(1000)

实现功能cpuchart.aspx.cs代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Web.UI.DataVisualization.Charting;
using System.Text;
public partial class demo_CpuChart : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    static PerformanceCounter pc = new PerformanceCounter("Processor", "% Processor Time", "_Total");
    protected void Timer1_Tick(object sender, EventArgs e)
    {
        MEMORY_INFO MemInfo = new MEMORY_INFO();
        ComputerInfo.GlobalMemoryStatus(ref MemInfo);
        //UseMemory
        Series series = ChartMemory.Series[0];
        int xCount = series.Points.Count == 0 ? 0 : series.Points.Count - 1;
        double lastXValue = series.Points.Count == 0 ? 1 : series.Points[xCount].XValue + 1;
        double lastYValue = (double)(MemInfo.dwTotalPhys - MemInfo.dwAvailPhys) / 1024 / 1024;
        series.Points.AddXY(lastXValue, lastYValue);
        //Total Memory
        series = ChartMemory.Series[1];
        lastYValue = (double)(MemInfo.dwTotalVirtual + MemInfo.dwTotalPhys - MemInfo.dwAvailPhys - MemInfo.dwAvailVirtual) / 1024 / 1024;
        series.Points.AddXY(lastXValue, lastYValue);
        //CPU
        series = ChartMemory.Series[2];
        lastYValue = (double)pc.NextValue();
        series.Points.AddXY(lastXValue, lastYValue);
        // Remove points from the left chart side if number of points exceeds 100.
        while (this.ChartMemory.Series[0].Points.Count > 80)
        {
            // Remove series points
            foreach (Series s in this.ChartMemory.Series)
            {
                s.Points.RemoveAt(0);
            }
        }
        // Adjust categorical scale
        double axisMinimum = this.ChartMemory.Series[0].Points[0].XValue;
        this.ChartMemory.ChartAreas[0].AxisX.Minimum = axisMinimum;
        this.ChartMemory.ChartAreas[0].AxisX.Maximum = axisMinimum + 99;
    }
    /// <summary>
    ///取得计算机的系统信息
    /// </summary>
    public class ComputerInfo
    {
        /// <summary>
        /// 取得Windows的目录
        /// </summary>
        /// <param name="WinDir"></param>
        /// <param name="count"></param>
        [DllImport("kernel32")]
        public static extern void GetWindowsDirectory(StringBuilder WinDir, int count);
        /// <summary>
        /// 获取系统路径
        /// </summary>
        /// <param name="SysDir"></param>
        /// <param name="count"></param>
        [DllImport("kernel32")]
        public static extern void GetSystemDirectory(StringBuilder SysDir, int count);
        /// <summary>
        /// 取得CPU信息
        /// </summary>
        /// <param name="cpuinfo"></param>
        [DllImport("kernel32")]
        public static extern void GetSystemInfo(ref CPU_INFO cpuinfo);
        /// <summary>
        /// 取得内存状态
        /// </summary>
        /// <param name="meminfo"></param>
        [DllImport("kernel32")]
        public static extern void GlobalMemoryStatus(ref MEMORY_INFO meminfo);
        /// <summary>
        /// 取得系统时间
        /// </summary>
        /// <param name="stinfo"></param>
        [DllImport("kernel32")]
        public static extern void GetSystemTime(ref SYSTEMTIME_INFO stinfo);
        public ComputerInfo()
        {
        }
    }
    //定义CPU的信息结构
    [StructLayout(LayoutKind.Sequential)]
    public struct CPU_INFO
    {
        public uint dwOemId;
        public uint dwPageSize;
        public uint lpMinimumApplicationAddress;
        public uint lpMaximumApplicationAddress;
        public uint dwActiveProcessorMask;
        public uint dwNumberOfProcessors;
        public uint dwProcessorType;
        public uint dwAllocationGranularity;
        public uint dwProcessorLevel;
        public uint dwProcessorRevision;
    }
    //定义内存的信息结构
    [StructLayout(LayoutKind.Sequential)]
    public struct MEMORY_INFO
    {
        public uint dwLength;
        public uint dwMemoryLoad;
        public uint dwTotalPhys;
        public uint dwAvailPhys;
        public uint dwTotalPageFile;
        public uint dwAvailPageFile;
        public uint dwTotalVirtual;
        public uint dwAvailVirtual;
    }
    //定义系统时间的信息结构
    [StructLayout(LayoutKind.Sequential)]
    public struct SYSTEMTIME_INFO
    {
        public ushort wYear;
        public ushort wMonth;
        public ushort wDayOfWeek;
        public ushort wDay;
        public ushort wHour;
        public ushort wMinute;
        public ushort wSecond;
        public ushort wMilliseconds;
    }
}

其中,MEMORY_INFO,ComputerInfo是一个定义的结构体及调用Win32 API接口的一个访问类。程序分别取得每一个图表对象,每次加载的时候,都重新取得当前的内存和Cpu信息

using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Web.UI.DataVisualization.Charting;
using System.Text;

以上几个为新引用的命名空间

为保证程序正常运行,还需要在C盘根目录下创建一个临时目录TempImageFiles,用来临时存放实时监控图片

微信公众号 weisico-com

微信公众号weisico-com

转载请注明:微思考学习网-关注技术,分享知识 >> ASP.NET微软图表控件MsChart实例之Cpu信息和内存使用监控图表

赞 (0) 收藏
分享到