示例#1
0
 private static bool ProcessHcsCall(int resultCode, string result)
 {
     if (resultCode == HcsException.PENDING)
     {
         return(true);
     }
     else if (HcsException.Failed(resultCode))
     {
         throw new HcsException(resultCode, result);
     }
     return(false);
 }
示例#2
0
        public HcsNotificationWatcher(IntPtr handle, RegisterHcsNotificationCallback register, UnregisterHcsNotificationCallback unregister, HCS_NOTIFICATIONS[] notificationList)
        {
            _unreg = unregister;

            foreach (var notificationType in notificationList)
            {
                var entry = new TaskCompletionSource <NotificationResult>();
                _n.Add(notificationType, entry);
            }

            _callbackFunc = (uint nType, IntPtr ctx, int nStatus, string nData) =>
            {
                var key = (HCS_NOTIFICATIONS)nType;
                if (key == HCS_NOTIFICATIONS.HcsNotificationServiceDisconnect)
                {
                    // Service disconnect should fail all outstanding notifications.
                    foreach (var entry in _n.Values)
                    {
                        entry.TrySetException(new HcsException(HcsException.E_ABORT, null));
                    }
                    return;
                }

                if (key == HCS_NOTIFICATIONS.HcsNotificationSystemExited && _n.ContainsKey(HCS_NOTIFICATIONS.HcsNotificationSystemStartCompleted))
                {
                    // Special handling for exit received while waiting for start.
                    _n[HCS_NOTIFICATIONS.HcsNotificationSystemStartCompleted].TrySetException(new HcsException(HcsException.UNEXPECTED_EXIT, null));
                }

                if (_n.ContainsKey(key))
                {
                    var result = new NotificationResult()
                    {
                        Status = nStatus,
                        Data   = nData
                    };

                    if (HcsException.Failed(result.Status))
                    {
                        _n[key].SetException(new HcsException(result.Status, result.Data));
                    }
                    else
                    {
                        _n[key].SetResult(result);
                    }
                }
            };

            HcsFunctions.ProcessHcsCall(register(handle, _callbackFunc, IntPtr.Zero, out _h), null);
        }