BeginRequestTime() public method

Asynchronously requests a time from the NTP server specified in the constructor. When a time is received the TimeReceived event is raised with the result, otherwise the ErrorOccurred event should be raised containing details of the failure.

Note, events raised by this class may not (and probably will not) occur on the same thread that called this method. If the event handlers call UI components, dispatched invoke may be required.

This method may throw exceptions (most likely a NtpNetworkException if an error occurs trying to connect/bind to the network endpoint. Exception handling in client code is recommended.

public BeginRequestTime ( ) : void
return void
Beispiel #1
0
        /// <summary>
        /// Returns an awaitable task whose result is the current time from the NTP server specified in the constructor.
        /// </summary>
        /// <remarks>
        /// <para>This method may throw exceptions (most likely a <seealso cref="NtpNetworkException"/> if an error occurs trying to connect/bind to the network endpoint. Exception handling in client code is recommended.</para>
        /// </remarks>
        /// <seealso cref="NtpNetworkException"/>
        public System.Threading.Tasks.Task <RequestTimeResult> RequestTimeAsync()
        {
            var tcs    = new System.Threading.Tasks.TaskCompletionSource <RequestTimeResult>();
            var client = new NtpClient(_ServerAddress);

            var timeReceivedHandler = new EventHandler <NtpTimeReceivedEventArgs>(
                (sender, args) =>
            {
                tcs.SetResult(new RequestTimeResult(args.CurrentTime, args.ReceivedAt));
            }
                );
            var errorOccurredHandler = new EventHandler <NtpNetworkErrorEventArgs>(
                (sender, args) =>
            {
                if (!tcs.Task.IsCanceled && !tcs.Task.IsCompleted)
                {
                    tcs.SetException(args.Exception);
                }
            }
                );

            client.TimeReceived  += timeReceivedHandler;
            client.ErrorOccurred += errorOccurredHandler;

            var retVal = tcs.Task;

            tcs.Task.ContinueWith(
                (pt) =>
            {
                client.TimeReceived  -= timeReceivedHandler;
                client.ErrorOccurred -= errorOccurredHandler;
            }
                );

            client.BeginRequestTime();

            return(retVal);
        }
Beispiel #2
0
		/// <summary>
		/// Returns an awaitable task whose result is the current time from the NTP server specified in the constructor.
		/// </summary>
		/// <remarks>
		/// <para>This method may throw exceptions (most likely a <seealso cref="NtpNetworkException"/> if an error occurs trying to connect/bind to the network endpoint. Exception handling in client code is recommended.</para>
		/// </remarks>
		/// <seealso cref="NtpNetworkException"/>
		public System.Threading.Tasks.Task<DateTime> RequestTimeAsync()
		{
			var tcs = new System.Threading.Tasks.TaskCompletionSource<DateTime>();
			var client = new NtpClient(_ServerAddress);

			var timeReceivedHandler = new EventHandler<NtpTimeReceivedEventArgs>(
				(sender, args) => 
				{
					tcs.SetResult(args.CurrentTime);
				}
			);
			var errorOccurredHandler = new EventHandler<NtpNetworkErrorEventArgs>(
				(sender, args) => 
				{
					if (!tcs.Task.IsCanceled && !tcs.Task.IsCompleted)
						tcs.SetException(args.Exception);
				}
			);

			client.TimeReceived += timeReceivedHandler;
			client.ErrorOccurred += errorOccurredHandler;

			var retVal = tcs.Task;
			tcs.Task.ContinueWith(
				(pt) =>
				{
					client.TimeReceived -= timeReceivedHandler;
					client.ErrorOccurred -= errorOccurredHandler;
				}
			);

			client.BeginRequestTime();

			return retVal;
		}