Click here to Skip to main content
15,894,291 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hey guys i have been trying from several days to code in C# to display all running task from task schedular but seems i am not succeeding in the same in case of OS win7 and the same code runs efficiently in win 8.1 OS.code is written below using wmi object. please if any one has alternative to this do share its a bit urgent.
C#
using System;
using System.Management;
using System.Windows.Forms;

namespace WMISample
{
    public class MyWMIQuery
    {
        public static void Main()
        {
            try
            {
                ManagementObjectSearcher searcher = 
                    new ManagementObjectSearcher("root\\Microsoft\\Windows\\TaskScheduler", 
                    "SELECT * FROM MSFT_ScheduledTask WHERE State = 4"); 

                foreach (ManagementObject queryObj in searcher.Get())
                {
                    Console.WriteLine("-----------------------------------");
                    Console.WriteLine("MSFT_ScheduledTask instance");
                    Console.WriteLine("-----------------------------------");
                    Console.WriteLine("State: {0}", queryObj["State"]);
                    Console.WriteLine("TaskName: {0}", queryObj["TaskName"]);
                }
            }
            catch (ManagementException e)
            {
                MessageBox.Show("An error occurred while querying for WMI data: " + e.Message);
            }
        }
    }
}
Note:state=4 stands for state running
Posted
Updated 3-Feb-15 2:56am
v2
Comments
CHill60 3-Feb-15 9:14am    
Shouldn't that be "\\\\ComputerName\\root\\Microsoft\\Windows\\TaskScheduler"
Herman<T>.Instance 3-Feb-15 10:19am    
is WMI service on on both machines? Does the User have sufficient rights?

I can't find the documentation, but chances are really good that it's because the MSFT_ScheduledTask class only exists on Windows 8.0 and above.

The approximate equivalent class in Vista/7 is Win32_ScheduledJob[^].
 
Share this answer
 
Comments
Herman<T>.Instance 3-Feb-15 16:50pm    
Nice one
CHill60 3-Feb-15 20:33pm    
I had a similar feeling and a similar struggle to find the documentation - good job on finding Win32_ScheduledJob! 5'd
no guys i tried it with win32_scheduledjob as well but the expected output is not displayed
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900