Esempio n. 1
0
 private void SetDeadline()
 {
     Fx.Assert(!_deadlineSet, "TimeoutHelper deadline set twice.");
     _deadline    = DateTime.UtcNow + _originalTimeout;
     _deadlineSet = true;
 }
Esempio n. 2
0
        public override bool Execute(ActivityExecutor executor, BookmarkManager bookmarkManager)
        {
            Fx.Assert("Empty work items should never been executed.");

            return(true);
        }
Esempio n. 3
0
 public FatalException(string message, Exception innerException) : base(message, innerException)
 {
     // This can't throw something like ArgumentException because that would be worse than
     // throwing the fatal exception that was requested.
     Fx.Assert(innerException == null || !Fx.IsFatal(innerException), "FatalException can't be used to wrap fatal exceptions.");
 }
Esempio n. 4
0
        private static bool TryNumericConversion <T>(object source, out T result)
        {
            Fx.Assert(source != null, "caller must verify");
            TypeCode sourceTypeCode      = source.GetType().GetTypeCode();
            TypeCode destinationTypeCode = typeof(T).GetTypeCode();

            switch (sourceTypeCode)
            {
            case TypeCode.SByte:
            {
                SByte sbyteSource = (SByte)source;
                switch (destinationTypeCode)
                {
                case TypeCode.Int16:
                    result = (T)(object)(Int16)sbyteSource;
                    return(true);

                case TypeCode.Int32:
                    result = (T)(object)(Int32)sbyteSource;
                    return(true);

                case TypeCode.Int64:
                    result = (T)(object)(Int64)sbyteSource;
                    return(true);

                case TypeCode.Single:
                    result = (T)(object)(Single)sbyteSource;
                    return(true);

                case TypeCode.Double:
                    result = (T)(object)(Double)sbyteSource;
                    return(true);

                case TypeCode.Decimal:
                    result = (T)(object)(Decimal)sbyteSource;
                    return(true);
                }
                break;
            }

            case TypeCode.Byte:
            {
                Byte byteSource = (Byte)source;
                switch (destinationTypeCode)
                {
                case TypeCode.Int16:
                    result = (T)(object)(Int16)byteSource;
                    return(true);

                case TypeCode.UInt16:
                    result = (T)(object)(UInt16)byteSource;
                    return(true);

                case TypeCode.Int32:
                    result = (T)(object)(Int32)byteSource;
                    return(true);

                case TypeCode.UInt32:
                    result = (T)(object)(UInt32)byteSource;
                    return(true);

                case TypeCode.Int64:
                    result = (T)(object)(Int64)byteSource;
                    return(true);

                case TypeCode.UInt64:
                    result = (T)(object)(UInt64)byteSource;
                    return(true);

                case TypeCode.Single:
                    result = (T)(object)(Single)byteSource;
                    return(true);

                case TypeCode.Double:
                    result = (T)(object)(Double)byteSource;
                    return(true);

                case TypeCode.Decimal:
                    result = (T)(object)(Decimal)byteSource;
                    return(true);
                }
                break;
            }

            case TypeCode.Int16:
            {
                Int16 int16Source = (Int16)source;
                switch (destinationTypeCode)
                {
                case TypeCode.Int32:
                    result = (T)(object)(Int32)int16Source;
                    return(true);

                case TypeCode.Int64:
                    result = (T)(object)(Int64)int16Source;
                    return(true);

                case TypeCode.Single:
                    result = (T)(object)(Single)int16Source;
                    return(true);

                case TypeCode.Double:
                    result = (T)(object)(Double)int16Source;
                    return(true);

                case TypeCode.Decimal:
                    result = (T)(object)(Decimal)int16Source;
                    return(true);
                }
                break;
            }

            case TypeCode.UInt16:
            {
                UInt16 uint16Source = (UInt16)source;
                switch (destinationTypeCode)
                {
                case TypeCode.Int32:
                    result = (T)(object)(Int32)uint16Source;
                    return(true);

                case TypeCode.UInt32:
                    result = (T)(object)(UInt32)uint16Source;
                    return(true);

                case TypeCode.Int64:
                    result = (T)(object)(Int64)uint16Source;
                    return(true);

                case TypeCode.UInt64:
                    result = (T)(object)(UInt64)uint16Source;
                    return(true);

                case TypeCode.Single:
                    result = (T)(object)(Single)uint16Source;
                    return(true);

                case TypeCode.Double:
                    result = (T)(object)(Double)uint16Source;
                    return(true);

                case TypeCode.Decimal:
                    result = (T)(object)(Decimal)uint16Source;
                    return(true);
                }
                break;
            }

            case TypeCode.Int32:
            {
                Int32 int32Source = (Int32)source;
                switch (destinationTypeCode)
                {
                case TypeCode.Int64:
                    result = (T)(object)(Int64)int32Source;
                    return(true);

                case TypeCode.Single:
                    result = (T)(object)(Single)int32Source;
                    return(true);

                case TypeCode.Double:
                    result = (T)(object)(Double)int32Source;
                    return(true);

                case TypeCode.Decimal:
                    result = (T)(object)(Decimal)int32Source;
                    return(true);
                }
                break;
            }

            case TypeCode.UInt32:
            {
                UInt32 uint32Source = (UInt32)source;
                switch (destinationTypeCode)
                {
                case TypeCode.UInt32:
                    result = (T)(object)(UInt32)uint32Source;
                    return(true);

                case TypeCode.Int64:
                    result = (T)(object)(Int64)uint32Source;
                    return(true);

                case TypeCode.UInt64:
                    result = (T)(object)(UInt64)uint32Source;
                    return(true);

                case TypeCode.Single:
                    result = (T)(object)(Single)uint32Source;
                    return(true);

                case TypeCode.Double:
                    result = (T)(object)(Double)uint32Source;
                    return(true);

                case TypeCode.Decimal:
                    result = (T)(object)(Decimal)uint32Source;
                    return(true);
                }
                break;
            }

            case TypeCode.Int64:
            {
                Int64 int64Source = (Int64)source;
                switch (destinationTypeCode)
                {
                case TypeCode.Single:
                    result = (T)(object)(Single)int64Source;
                    return(true);

                case TypeCode.Double:
                    result = (T)(object)(Double)int64Source;
                    return(true);

                case TypeCode.Decimal:
                    result = (T)(object)(Decimal)int64Source;
                    return(true);
                }
                break;
            }

            case TypeCode.UInt64:
            {
                UInt64 uint64Source = (UInt64)source;
                switch (destinationTypeCode)
                {
                case TypeCode.Single:
                    result = (T)(object)(Single)uint64Source;
                    return(true);

                case TypeCode.Double:
                    result = (T)(object)(Double)uint64Source;
                    return(true);

                case TypeCode.Decimal:
                    result = (T)(object)(Decimal)uint64Source;
                    return(true);
                }
                break;
            }

            case TypeCode.Char:
            {
                Char charSource = (Char)source;
                switch (destinationTypeCode)
                {
                case TypeCode.UInt16:
                    result = (T)(object)(UInt16)charSource;
                    return(true);

                case TypeCode.Int32:
                    result = (T)(object)(Int32)charSource;
                    return(true);

                case TypeCode.UInt32:
                    result = (T)(object)(UInt32)charSource;
                    return(true);

                case TypeCode.Int64:
                    result = (T)(object)(Int64)charSource;
                    return(true);

                case TypeCode.UInt64:
                    result = (T)(object)(UInt64)charSource;
                    return(true);

                case TypeCode.Single:
                    result = (T)(object)(Single)charSource;
                    return(true);

                case TypeCode.Double:
                    result = (T)(object)(Double)charSource;
                    return(true);

                case TypeCode.Decimal:
                    result = (T)(object)(Decimal)charSource;
                    return(true);
                }
                break;
            }

            case TypeCode.Single:
            {
                if (destinationTypeCode == TypeCode.Double)
                {
                    result = (T)(object)(Double)(Single)source;
                    return(true);
                }
                break;
            }
            }

            result = default(T);
            return(false);
        }
Esempio n. 5
0
        protected void Complete(bool completedSynchronously)
        {
            if (_isCompleted)
            {
                throw Fx.Exception.AsError(new InvalidOperationException(InternalSR.AsyncResultCompletedTwice(GetType())));
            }

            //#if DEBUG
            //            this.marker.AsyncResult = null;
            //            this.marker = null;
            //            if (!Fx.FastDebug && completeStack == null)
            //            {
            //                completeStack = new StackTrace();
            //            }
            //#endif

            _completedSynchronously = completedSynchronously;
            if (OnCompleting != null)
            {
                // Allow exception replacement, like a catch/throw pattern.
                try
                {
                    OnCompleting(this, _exception);
                }
                catch (Exception exception)
                {
                    if (Fx.IsFatal(exception))
                    {
                        throw;
                    }
                    _exception = exception;
                }
            }

            if (completedSynchronously)
            {
                // If we completedSynchronously, then there's no chance that the manualResetEvent was created so
                // we don't need to worry about a race condition
                Fx.Assert(_manualResetEvent == null, "No ManualResetEvent should be created for a synchronous AsyncResult.");
                _isCompleted = true;
            }
            else
            {
                lock (ThisLock)
                {
                    _isCompleted = true;
                    if (_manualResetEvent != null)
                    {
                        _manualResetEvent.Set();
                    }
                }
            }

            if (_callback != null)
            {
                try
                {
                    if (VirtualCallback != null)
                    {
                        VirtualCallback(_callback, this);
                    }
                    else
                    {
                        _callback(this);
                    }
                }
#pragma warning disable 1634
#pragma warning suppress 56500 // transferring exception to another thread
                catch (Exception e)
                {
                    if (Fx.IsFatal(e))
                    {
                        throw;
                    }

                    throw Fx.Exception.AsError(new CallbackException(InternalSR.AsyncCallbackThrewException, e));
                }
#pragma warning restore 1634
            }
        }