C# CIMException bei Async Task

Magic1416

Lieutenant
Registriert
Dez. 2003
Beiträge
530
Hallo Zusammen,

ich habe folgendes Problem.
Manchmal erhalte ich bei den unten genannten Funktionen den folgenden Fehler. Ich kann mir den Fehler nicht erklären und ich bekomme ihn auch nicht gelöst. Die meiste Zeit funktioniert der Code. Deshalb ist er auch noch nie aufgetreten, während ein Debugger aktiv war :

Microsoft.Management.Infrastructure.CimException: The I/O operation has been aborted because of either a thread exit or an application request.
at Microsoft.Management.Infrastructure.Internal.Operations.CimSyncEnumeratorBase`1.MoveNext()
at mvRDSMgmt.mvRDSManagement.GetRDSSessionsWMI(

Die folgende Methode wird von einem Timer regelmäßig aufgerufen. Ein Background Task ruft über WMI die Sessions ab. Danach aktualisiert der Vordergrund Task eine BindingList, welche an ein Datagridview gebunden ist.


C#:
/// <summary>
        /// gets the Sessions asynchronously
        /// </summary>
        /// <returns></returns>
        private async Task getSessionsAsync()
        {           
            //Set errortext
            string errorText = string.Empty;

            //abort while already refreshing
            if (!_refreshRunning)
            {
              
                //Set Refresh to true
                setRefreshRunning(true);
                
                //Set Statusbar
                barStaticItem1.Caption  = "Refreshing ...";

                //Set Task Parameters
                var backgroundScheduler = TaskScheduler.Default;
                var uiScheduler         = TaskScheduler.FromCurrentSynchronizationContext();
                var cancelToken         = CancellationToken.None;

                //Query RDS Host. Create Async Task
                //Der Fehler tritt in der Methode GetRDSSessionsWMI auf
                await Task<mvRDSSessions>.Factory.StartNew(
                        function            : () => { return mvRDSManagement.GetRDSSessionsWMI(_rdsserver, _collection, ref errorText); },  
                        cancellationToken   : cancelToken,
                        creationOptions     : TaskCreationOptions.None,
                        scheduler           : backgroundScheduler)                   
                     .ContinueWith(
                        continuationAction  : rdsQueryTask2 => {mvRDSManagement.UpdateDataSourceSessions(_bindingList1, rdsQueryTask2.Result);},
                        cancellationToken   : cancelToken,
                        continuationOptions : TaskContinuationOptions.None,
                        scheduler           : uiScheduler);
                      
                

                //Set Statusbar
                update_StatusMessage(errorText);
                
                //Set Refresh to false
                setRefreshRunning(false);
            }         
        }



C#:
using Microsoft.Management.Infrastructure;
public static mvRDSSessions GetRDSSessionsWMI(string servernameFQDN, string collection, ref string errorText)
        {
            //Create RDS Session
            var rdsSessions = new mvRDSSessions();

            //Set Query
            string OSQuery = "SELECT * FROM Win32_SessionDirectorySessionEx";

            //Add Filter
            if (!string.IsNullOrWhiteSpace(collection))
                OSQuery += $" where CollectionAlias like '{collection}'";

            //Create Result
            var result = wmiQuery(servernameFQDN, @"root\cimv2", OSQuery, ref errorText);
            if (result != null)
            {
                foreach (var item in result)
                {
                    try
                    {

                        rdsSessions.Add(cimObjectToRdsSession(item));

                    }
                    catch(Exception ex)
                    {
                        Logging.Log._logFile.ShowMessage($"Error with cim object {item.CimInstanceProperties["UserName"].Value.ToString()}. {ex.Message}","");
                    }
                }
            }
          
            //Result
            return rdsSessions;

        }


private static IEnumerable<CimInstance> wmiQuery(string computer, string ns, string query, ref string errorText)
        {
            lock (queryLock)
            {
                var mySession = CimSession.Create(computer);

              
                try
                {
                    return mySession.QueryInstances(ns, "WQL", query);

                }
                catch (Exception ex)
                {
                    errorText = ex.Message;
                }

                
            }
            return null;
        }
        static object queryLock = new object();


 private static mvRDSSession cimObjectToRdsSession(CimInstance o)
        {
            //Set session
            var app = new mvRDSSession();

            //Get Props
            app.UserName            = o.CimInstanceProperties["UserName"].Value.ToString();
            app.ServerName          = o.CimInstanceProperties["ServerName"].Value.ToString();
            app.SessionID           = (UInt32)o.CimInstanceProperties["SessionID"].Value;
            app.DomainName          = o.CimInstanceProperties["DomainName"].Value.ToString();
            app.ServerIPAddress     = o.CimInstanceProperties["ServerIPAddress"].Value.ToString();
            app.TSProtocol          = (UInt32)o.CimInstanceProperties["TSProtocol"].Value;
            app.ResolutionWidth     = (UInt32)o.CimInstanceProperties["ResolutionWidth"].Value;
            app.ResolutionHeight    = (UInt32)o.CimInstanceProperties["ResolutionHeight"].Value;
            app.ColorDepth          = (UInt32)o.CimInstanceProperties["ColorDepth"].Value;
            app.CreateTime = (DateTime)o.CimInstanceProperties["CreateTime"].Value;

            UInt32 state = (UInt32)o.CimInstanceProperties["SessionState"].Value;
            SESSION_STATE s = (SESSION_STATE)state;

            app.SessionState        = (s).ToString();
            app.CollectionName      = o.CimInstanceProperties["CollectionAlias"].Value.ToString();
            app.CollectionType      = o.CimInstanceProperties["CollectionType"].Value.ToString();
            app.HostServer          = o.CimInstanceProperties["HostServer"].Value.ToString();
            app.IdleTime            = (UInt32)o.CimInstanceProperties["IdleTime"].Value;


            //Set Disconnect Time
            if (o.CimInstanceProperties["DisconnectTime"].Value != null)
                app.DisconnectTime = (DateTime)o.CimInstanceProperties["DisconnectTime"].Value;

            //Set unified Session ID
            if (o.CimInstanceProperties["UnifiedSessionID"].Value == null)
                app.UnifiedSessionID = 0;
            else
                app.UnifiedSessionID = (UInt32)o.CimInstanceProperties["UnifiedSessionID"].Value;

            //Set RemoteFX
            if (o.CimInstanceProperties["RemoteFXEnabled"].Value == null)
                app.RemoteFXEnabled = false;
            else
                app.RemoteFXEnabled = (bool)o.CimInstanceProperties["RemoteFXEnabled"].Value;

            //Set Updated
            app.Updated = true;

            //return
            return app;           
        }


Hat jemand von Euch eine Idee, was ich falsch mache ?

Danke, Gruß Magic
 
Ich habe leider keinen Errorcode. Ich bekomme den Fehler selbst nicht. Diejenigen, die die Anwendung benutzen schicken mir Screenshots vom Fehler. Welche Möglichkeiten des Tracing habe ich denn, um die Fehlerursache besser einschränken zu können.
 
Naja, dann musst du halt die Stelle rausfinden wo es auftritt und entsprechend die Exception wegloggen.
Grundsätzlich solltest du ja den Fehler eh erstmal selbst reproduzieren können. Ich denke dafür ist der Beitrag von Stackoverflow schon recht brauchbar als Basis
 
Zurück
Oben