当前位置: 首页>>代码示例>>C#>>正文


C# AsyncContinuation类代码示例

本文整理汇总了C#中AsyncContinuation的典型用法代码示例。如果您正苦于以下问题:C# AsyncContinuation类的具体用法?C# AsyncContinuation怎么用?C# AsyncContinuation使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


AsyncContinuation类属于命名空间,在下文中一共展示了AsyncContinuation类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: FlushAsync

		protected override void FlushAsync(AsyncContinuation asyncContinuation)
		{
			foreach (var log in logs)
			{
				WrappedTarget.WriteAsyncLogEvent(log);
			}

			logs.Clear();
			base.FlushAsync(asyncContinuation);
		}
开发者ID:304NotModified,项目名称:NLog.ManualFlush,代码行数:10,代码来源:ManualFlushWrapper.cs

示例2: TestEquals

 public void TestEquals()
 {
     var logEvent1 = new LogEventInfo(LogLevel.Debug, "logger1", "message1");
     AsyncContinuation cont1 = new AsyncContinuation(exception => { });
     var async1 = new AsyncLogEventInfo(logEvent1, cont1);
     var async2 = new AsyncLogEventInfo(logEvent1, cont1);
     Assert.True(async1.Equals(async2));
     Assert.True(async1 == async2);
     Assert.False(async1 != async2);
     Assert.Equal(async1.GetHashCode(), async2.GetHashCode());
 }
开发者ID:daniefer,项目名称:NLog,代码行数:11,代码来源:AsyncLogEventInfoTests.cs

示例3: DoSend

        /// <summary>
        /// Actually sends the given text over the specified protocol.
        /// </summary>
        /// <param name="bytes">The bytes to be sent.</param>
        /// <param name="offset">Offset in buffer.</param>
        /// <param name="length">Number of bytes to send.</param>
        /// <param name="asyncContinuation">The async continuation to be invoked after the buffer has been sent.</param>
        /// <remarks>To be overridden in inheriting classes.</remarks>
        protected override void DoSend(byte[] bytes, int offset, int length, AsyncContinuation asyncContinuation)
        {
            var webRequest = WebRequest.Create(new Uri(this.Address));
            webRequest.Method = "POST";

            AsyncCallback onResponse =
                r =>
                {
                    try
                    {
                        using (var response = webRequest.EndGetResponse(r))
                        {
                        }

                        // completed fine
                        asyncContinuation(null);
                    }
                    catch (Exception ex)
                    {
                        if (ex.MustBeRethrown())
                        {
                            throw;
                        }

                        asyncContinuation(ex);
                    }
                };

            AsyncCallback onRequestStream =
                r =>
                {
                    try
                    {
                        using (var stream = webRequest.EndGetRequestStream(r))
                        {
                            stream.Write(bytes, offset, length);
                        }

                        webRequest.BeginGetResponse(onResponse, null);
                    }
                    catch (Exception ex)
                    {
                        if (ex.MustBeRethrown())
                        {
                            throw;
                        }

                        asyncContinuation(ex);
                    }
                };

            webRequest.BeginGetRequestStream(onRequestStream, null);
        }
开发者ID:CharlieBP,项目名称:NLog,代码行数:61,代码来源:HttpNetworkSender.cs

示例4: Flush

        /// <summary>
        /// Flush any pending log messages (in case of asynchronous targets).
        /// </summary>
        /// <param name="asyncContinuation">The asynchronous continuation.</param>
        public void Flush(AsyncContinuation asyncContinuation)
        {
            asyncContinuation = AsyncHelpers.OneTimeOnly(asyncContinuation);

            try
            {
                this.FlushAsync(asyncContinuation);
            }
            catch (Exception ex)
            {
                asyncContinuation(ex);
            }
        }
开发者ID:igalse,项目名称:NLog,代码行数:17,代码来源:Target.cs

示例5: DoClose

 /// <summary>
 ///     Closes the socket.
 /// </summary>
 /// <param name="continuation">The continuation.</param>
 protected override void DoClose(AsyncContinuation continuation)
 {
     lock (this)
     {
         if (asyncOperationInProgress)
         {
             closeContinuation = continuation;
         }
         else
         {
             CloseSocket(continuation);
         }
     }
 }
开发者ID:modulexcite,项目名称:SQLoogle,代码行数:18,代码来源:TcpNetworkSender.cs

示例6: TestNotEquals

        public void TestNotEquals()
        {
            var logEvent1 = new LogEventInfo(LogLevel.Debug, "logger1", "message1");
            AsyncContinuation cont1 = new AsyncContinuation(exception => { });
            AsyncContinuation cont2 = new AsyncContinuation(exception => { InternalLogger.Debug("test"); });
            var async1 = new AsyncLogEventInfo(logEvent1, cont1);
            var async2 = new AsyncLogEventInfo(logEvent1, cont2);
            Assert.False(async1.Equals(async2));
            Assert.False(async1 == async2);
            Assert.True(async1 != async2);

            //2 delegates will return the same hashcode, http://stackoverflow.com/questions/6624151/why-do-2-delegate-instances-return-the-same-hashcode
            //and that isn't really bad, so ignore this
            //   Assert.NotEqual(async1.GetHashCode(), async2.GetHashCode());
        }
开发者ID:daniefer,项目名称:NLog,代码行数:15,代码来源:AsyncLogEventInfoTests.cs

示例7: DoInvoke

        /// <summary>
        ///     Calls the target method. Must be implemented in concrete classes.
        /// </summary>
        /// <param name="parameters">Method call parameters.</param>
        /// <param name="continuation">The continuation.</param>
        protected virtual void DoInvoke(object[] parameters, AsyncContinuation continuation)
        {
            try
            {
                DoInvoke(parameters);
                continuation(null);
            }
            catch (Exception ex)
            {
                if (ex.MustBeRethrown())
                {
                    throw;
                }

                continuation(ex);
            }
        }
开发者ID:modulexcite,项目名称:SQLoogle,代码行数:22,代码来源:MethodCallTargetBase.cs

示例8: PostFilteringTargetWrapperNoFiltersDefined

        public void PostFilteringTargetWrapperNoFiltersDefined()
        {
            var target = new MyTarget();
            var wrapper = new PostFilteringTargetWrapper()
            {
                WrappedTarget = target,
            };

            ((ISupportsInitialize)wrapper).Initialize();
            ((ISupportsInitialize)target).Initialize();

            var events = new LogEventInfo[]
            {
                new LogEventInfo(LogLevel.Debug, "Logger1", "Hello"),
                new LogEventInfo(LogLevel.Info, "Logger1", "Hello"),
                new LogEventInfo(LogLevel.Info, "Logger2", "Hello"),
                new LogEventInfo(LogLevel.Debug, "Logger1", "Hello"),
                new LogEventInfo(LogLevel.Trace, "Logger1", "Hello"),
                new LogEventInfo(LogLevel.Info, "Logger3", "Hello"),
                new LogEventInfo(LogLevel.Error, "Logger1", "Hello"),
            };

            var exceptions = new List<Exception>();

            var continuations = new AsyncContinuation[events.Length];
            for (int i = 0; i < continuations.Length; ++i)
            {
                continuations[i] = exceptions.Add;
            }

            wrapper.WriteLogEvents(events, continuations);

            // make sure all events went through
            Assert.AreEqual(7, target.Events.Count);
            Assert.AreSame(events[0], target.Events[0]);
            Assert.AreSame(events[1], target.Events[1]);
            Assert.AreSame(events[2], target.Events[2]);
            Assert.AreSame(events[3], target.Events[3]);
            Assert.AreSame(events[4], target.Events[4]);
            Assert.AreSame(events[5], target.Events[5]);
            Assert.AreSame(events[6], target.Events[6]);

            Assert.AreEqual(continuations.Length, exceptions.Count, "Some continuations were not invoked.");
        }
开发者ID:igalse,项目名称:NLog,代码行数:44,代码来源:PostFilteringTargetWrapperTests.cs

示例9: RetryingTargetWrapperTest1

        public void RetryingTargetWrapperTest1()
        {
            var target = new MyTarget();
            var wrapper = new RetryingTargetWrapper()
            {
                WrappedTarget = target,
                RetryCount = 10,
                RetryDelayMilliseconds = 1,
            };

            ((ISupportsInitialize)wrapper).Initialize();
            ((ISupportsInitialize)target).Initialize();

            var events = new LogEventInfo[]
            {
                new LogEventInfo(LogLevel.Debug, "Logger1", "Hello"),
                new LogEventInfo(LogLevel.Info, "Logger1", "Hello"),
                new LogEventInfo(LogLevel.Info, "Logger2", "Hello"),
            };

            var exceptions = new List<Exception>();

            var continuations = new AsyncContinuation[events.Length];
            for (int i = 0; i < continuations.Length; ++i)
            {
                continuations[i] = exceptions.Add;
            }

            wrapper.WriteLogEvents(events, continuations);

            // make sure all events went through
            Assert.AreEqual(3, target.Events.Count);
            Assert.AreSame(events[0], target.Events[0]);
            Assert.AreSame(events[1], target.Events[1]);
            Assert.AreSame(events[2], target.Events[2]);

            Assert.AreEqual(continuations.Length, exceptions.Count, "Some continuations were not invoked.");

            // make sure there were no exception
            foreach (var ex in exceptions)
            {
                Assert.IsNull(ex);
            }
        }
开发者ID:igalse,项目名称:NLog,代码行数:44,代码来源:RetryingTargetWrapperTests.cs

示例10: RepeatingTargetWrapperTest1

        public void RepeatingTargetWrapperTest1()
        {
            var target = new MyTarget();
            var wrapper = new RepeatingTargetWrapper()
            {
                WrappedTarget = target,
                RepeatCount = 3,
            };
            ((ISupportsInitialize)wrapper).Initialize();
            ((ISupportsInitialize)target).Initialize();

            var events = new LogEventInfo[]
            {
                new LogEventInfo(LogLevel.Debug, "Logger1", "Hello"),
                new LogEventInfo(LogLevel.Info, "Logger1", "Hello"),
                new LogEventInfo(LogLevel.Info, "Logger2", "Hello"),
            };

            var exceptions = new List<Exception>();

            var continuations = new AsyncContinuation[events.Length];
            for (int i = 0; i < continuations.Length; ++i)
            {
                continuations[i] = exceptions.Add;
            }

            wrapper.WriteLogEvents(events, continuations);

            // make sure all events went through and were replicated 3 times
            Assert.AreEqual(9, target.Events.Count);
            Assert.AreSame(events[0], target.Events[0]);
            Assert.AreSame(events[0], target.Events[1]);
            Assert.AreSame(events[0], target.Events[2]);
            Assert.AreSame(events[1], target.Events[3]);
            Assert.AreSame(events[1], target.Events[4]);
            Assert.AreSame(events[1], target.Events[5]);
            Assert.AreSame(events[2], target.Events[6]);
            Assert.AreSame(events[2], target.Events[7]);
            Assert.AreSame(events[2], target.Events[8]);

            Assert.AreEqual(continuations.Length, exceptions.Count, "Some continuations were not invoked.");
        }
开发者ID:igalse,项目名称:NLog,代码行数:42,代码来源:RepeatingTargetWrapperTests.cs

示例11: DoInvoke

        protected override void DoInvoke(object[] parameters, AsyncContinuation continuation)
        {
            IRestApiClient client = _clientFactory();

            Task<HttpResponseMessage> task;
            switch(HttpMethod.ToLower())
            {
            case "post":
                task = client.PostAsync(Url, GetByteContent(parameters));
                break;

            case "put":
                task = client.PutAsync(Url, GetByteContent(parameters));
                break;

            case "get":
                task = client.GetAsync(new Uri(Url, GetQueryContent(parameters)));
                break;

            default:
                continuation(new Exception("Unsupported HTTP method"));
                return;
            }

            task.ContinueWith(t =>
                              {
                                  try
                                  {
                                      t.Result.EnsureSuccessStatusCode();
                                      continuation(null);
                                  }
                                  catch(Exception ex)
                                  {
                                      continuation(ex);
                                  }
                                  finally
                                  {
                                      client.Dispose();
                                  }
                              });
        }
开发者ID:CodingGorilla,项目名称:NLog-Contrib,代码行数:41,代码来源:RestApiTarget.cs

示例12: DoClose

        /// <summary>
        ///     Closes the socket.
        /// </summary>
        /// <param name="continuation">The continuation.</param>
        protected override void DoClose(AsyncContinuation continuation)
        {
            lock (this)
            {
                try
                {
                    if (socket != null)
                    {
                        socket.Close();
                    }
                }
                catch (Exception exception)
                {
                    if (exception.MustBeRethrown())
                    {
                        throw;
                    }
                }

                socket = null;
            }
        }
开发者ID:modulexcite,项目名称:SQLoogle,代码行数:26,代码来源:UdpNetworkSender.cs

示例13: SingleCallContinuation

 /// <summary>
 /// Initializes a new instance of the <see cref="SingleCallContinuation"/> class.
 /// </summary>
 /// <param name="asyncContinuation">The asynchronous continuation.</param>
 public SingleCallContinuation(AsyncContinuation asyncContinuation)
 {
     this.asyncContinuation = asyncContinuation;
 }
开发者ID:semirs,项目名称:CellAO,代码行数:8,代码来源:SingleCallContinuation.cs

示例14: DoInvoke

        internal void DoInvoke(object[] parameters, AsyncContinuation continuation, HttpWebRequest request, Func<AsyncCallback, IAsyncResult> beginFunc, 
            Func<IAsyncResult, Stream> getStreamFunc)
        {
            Stream postPayload = null;

            switch (this.Protocol)
            {
                case WebServiceProtocol.Soap11:
                    postPayload = this.PrepareSoap11Request(request, parameters);
                    break;

                case WebServiceProtocol.Soap12:
                    postPayload = this.PrepareSoap12Request(request, parameters);
                    break;

                case WebServiceProtocol.HttpGet:
                    this.PrepareGetRequest(request);
                    break;

                case WebServiceProtocol.HttpPost:
                    postPayload = this.PreparePostRequest(request, parameters);
                    break;
            }

            AsyncContinuation sendContinuation =
                ex =>
                {
                    if (ex != null)
                    {
                        continuation(ex);
                        return;
                    }

                    request.BeginGetResponse(
                        r =>
                        {
                            try
                            {
                                using (var response = request.EndGetResponse(r))
                                {
                                }

                                continuation(null);
                            }
                            catch (Exception ex2)
                            {
                                InternalLogger.Error(ex2, "Error when sending to Webservice.");

                                if (ex2.MustBeRethrown())
                                {
                                    throw;
                                }

                                continuation(ex2);
                            }
                        },
                        null);
                };

            if (postPayload != null && postPayload.Length > 0)
            {
                postPayload.Position = 0;
                beginFunc(
                    result =>
                    {
                        try
                        {
                            using (Stream stream = getStreamFunc(result))
                            {
                                WriteStreamAndFixPreamble(postPayload, stream, this.IncludeBOM, this.Encoding);

                                postPayload.Dispose();
                            }

                            sendContinuation(null);
                        }
                        catch (Exception ex)
                        {
                            postPayload.Dispose();
                            InternalLogger.Error(ex, "Error when sending to Webservice.");

                            if (ex.MustBeRethrown())
                            {
                                throw;
                            }

                            continuation(ex);
                        }
                    });
            }
            else
            {
                sendContinuation(null);
            }
        }
开发者ID:MikeFH,项目名称:NLog,代码行数:95,代码来源:WebServiceTarget.cs

示例15: DoSend

 protected override void DoSend(byte[] bytes, int offset, int length, AsyncContinuation asyncContinuation)
 {
     this.log.WriteLine("{0}: send {1} {2}", this.id, offset, length);
     this.MemoryStream.Write(bytes, offset, length);
     if (this.senderFactory.FailCounter > 0)
     {
         this.log.WriteLine("{0}: failed", this.id);
         this.senderFactory.FailCounter--;
         asyncContinuation(new IOException("some IO error has occured"));
     }
     else
     {
         asyncContinuation(null);
     }
 }
开发者ID:rameshr,项目名称:NLog,代码行数:15,代码来源:NetworkTargetTests.cs


注:本文中的AsyncContinuation类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。