0

I have c# code that uses the following

ThreadPool.QueueUserWorkItem(new WaitCallback(parseEventLogsWmi), new object[] { server } );

and I have a server that is causing the code to hang and eventually fail because the WMi query doesn't finish in a timely manner. I believe something is wrong with WMI on the server, regardless I need a way to handle this and the timeout functions of WMI are not working. I'll get a timeout error or quota error from WMI eventually.

var conOpt = new ConnectionOptions();
conOpt.Impersonation = ImpersonationLevel.Impersonate;
conOpt.EnablePrivileges = true;
var scope = new ManagementScope(String.Format(@"\\{0}\ROOT\CIMV2", server), conOpt);

SelectQuery query = new SelectQuery("Select * from Win32_NTLogEvent Where Logfile = 'System' and TimeGenerated >='" + dateTime + "'");

ManagementObjectSearcher searcher = new 
ManagementObjectSearcher(scope, query);
searcher.Options.Timeout = new TimeSpan(0, 0, 30);
searcher.Options.ReturnImmediately = true;

Is there a working way to limit the results back from WMI to 1000 results or get the timeout feature to work, or get the remaing threads left in a threadpool after a time limit, and terminate the thread in the threadpool?

Bbb
  • 517
  • 6
  • 27
  • What is your version of .net? – Oleg Oct 03 '19 at 15:24
  • @Oleg it's 4.7.02558 – Bbb Oct 03 '19 at 15:41
  • I don't think you can kill a function that is started via the threadpool. With `Task` based functions, they have a `CancellationToken` which can be used to stop it. – Neil Oct 03 '19 at 15:46
  • 1
    With 4.7 you can work with task, and it also work with threadpool and can be canceled. – Oleg Oct 03 '19 at 15:51
  • Does anyone have any reasonable simple examples of converting a threadpool to a task (or task pool)? I have a lot of code revolving around the threadpool and I would like to see if it's feasible to convert it. – Bbb Oct 03 '19 at 16:18
  • Look at https://stackoverflow.com/questions/4238345/asynchronously-wait-for-taskt-to-complete-with-timeout – Oleg Oct 03 '19 at 16:24
  • I'm not sure how to integrate that into my thread pool and/or existing code, so I can't use that solution. – Bbb Oct 03 '19 at 19:37

1 Answers1

0

Here is how I got around the WMI hanging issue, I used the eventLogQuery .net object with the existing thread structure I have. The eventLogQuery structure was a pita to figure out, but I have that working as well as shown below for searches.

// level = 1 is critical, level = 2 is error, level = 3 is warning and level = 4 is information
            // 86400000 is 1 day in milliseconds
            // 259200000 is 3 days in milliseconds
            // 2592000000 is 30 days in milliseconds

            string queryString = "<QueryList>" +
            "  <Query Id=\"0\" Path=\"System\">" +
            "    <Select Path=\"System\">" +
            "        *[System[(Level=1 or Level=2) and" +
            "        TimeCreated[timediff(@SystemTime) &lt; " + historicalDaysInMilliseconds + "]]]" +
            "    </Select>" +
            "  </Query>" +
            "</QueryList>";
            EventLogSession session = new EventLogSession(server);
            EventLogQuery evtquery = new EventLogQuery("System", PathType.LogName, queryString) { ReverseDirection = true };
            evtquery.Session = session;

            EventLogReader logReader = new EventLogReader(evtquery);

            EventRecord entry;
            List<string> eventArr = new List<string>();
            while ((entry = logReader.ReadEvent()) != null)
            {
                foreach (string eventId in events)
                {
                    if (eventId == entry.Id.ToString())
                    {
                        //eventArr.Add(entry.Id.ToString() + " - " + entry.MachineName + " - " + entry.TimeCreated.Value.ToString());
                        Console.WriteLine(entry.Id.ToString() + " - " + entry.MachineName + " - " + entry.TimeCreated.Value.ToString() + " - " + eventArr.Count);
                        returnValue.Add(server + ", " + entry.Id.ToString() + ", " + entry.TimeCreated.Value.ToString());
                        stop = "here";
                    }
                }
            }
Bbb
  • 517
  • 6
  • 27