Esempio n. 1
0
        void WriteToQueue(object state)
        {
            try
            {
                if (!_clientStream.IsConnected)
                {
                    _clientStream.Connect((int)_connectionRetryInterval.TotalMilliseconds);
                }

                object message;
                while (_readQueue.TryDequeue(out message))
                {
                    var ms = new MemoryStream();
                    _formatter.Serialize(ms, message);
                    var bytes = ms.ToArray();
                    _clientStream.Write(bytes, 0, bytes.Length);
                    _clientStream.Flush();
                }

                ResetTimer();
            }
            catch (TimeoutException timeoutException)
            {
                Trace("Could not connect to profiler: {0}", timeoutException.Message);
                if (DateTime.Now - _initTime < _giveUpInterval)
                {
                    ResetTimer();
                }
                else
                {
                    Trace("Connection attempt threshold exceeded; detaching event queue and giving up.");
                    var nullQueue = new NullQueue();
                    _readQueue  = nullQueue;
                    _writeQueue = nullQueue;
                    Dispose();
                }
            }
            catch (Exception exception)
            {
                Trace("Error communicating with profiler, giving up: {0}", exception.Message);
            }
        }
        void WriteToQueue(object state)
        {
            try
            {
                if (!_clientStream.IsConnected)
                    _clientStream.Connect((int)_connectionRetryInterval.TotalMilliseconds);

                object message;
                while (_readQueue.TryDequeue(out message))
                {
                    var ms = new MemoryStream();
                    _formatter.Serialize(ms, message);
                    var bytes = ms.ToArray();
                    _clientStream.Write(bytes, 0, bytes.Length);
                    _clientStream.Flush();
                }

                ResetTimer();
            }
            catch (TimeoutException timeoutException)
            {
                Trace("Could not connect to profiler: {0}", timeoutException.Message);
                if (DateTime.Now - _initTime < _giveUpInterval)
                {
                    ResetTimer();
                }
                else
                {
                    Trace("Connection attempt threshold exceeded; detaching event queue and giving up.");
                    var nullQueue = new NullQueue();
                    _readQueue = nullQueue;
                    _writeQueue = nullQueue;
                    Dispose();
                }
            }
            catch (Exception exception)
            {
                Trace("Error communicating with profiler, giving up: {0}", exception.Message);
            }
        }