private void hddThreadActivity() { using (ManagementClass physicalDriveData = new ManagementClass("Win32_PerfFormattedData_PerfDisk_PhysicalDisk")) { //Using WMI to get physical disk activity per second. while (true) { //Queries WMI at each itteration and gets the new values. //Gets the instances of all physical drives in the system(separately) and a Total instance (all drives on the system combined). ManagementObjectCollection physicalDriveDataCollection = physicalDriveData.GetInstances(); //finds total instance foreach (ManagementObject obj in physicalDriveDataCollection) { //Process only the _Total instance, and ignore all the others. if (obj["Name"].ToString() == "_Total") { //Getting the DiskBytesPersec property //unsigned 64bit intenger,return value needs to be converted. if (Convert.ToUInt64(obj["DIskBytesPersec"]) > 0) { if (previousHddState == HddState.Idle) { //Show busy icon. hddTrayIcon.Icon = busyIcon; } previousHddState = HddState.Busy; } else { if (previousHddState == HddState.Busy) { //Show idle icon. hddTrayIcon.Icon = idleIcon; } previousHddState = HddState.Idle; } } } if (exitRequested) { return; } else { //Sleep for 10th of a second Thread.Sleep(100); } } } }
private void setUpIcons() { //Instantiating icon objects, and joining adequate .ico images. busyIcon = new Icon("images\\HDD_Busy.ico"); idleIcon = new Icon("images\\HDD_Idle.ico"); hddTrayIcon = new NotifyIcon(); //Assinging default idleIcon to the tray icon and making it visible. hddTrayIcon.Icon = idleIcon; hddTrayIcon.Visible = true; previousHddState = HddState.Idle; }