/// <summary>
 /// Invokes the specified async event on another thread
 /// </summary>
 public static async Task TimedInvokeAsync <TArgs>(this AsyncEventHandler <TArgs> eventHandler, object source, TArgs arg, int timeout, Logger log) where TArgs : EventArgs
 {
     if (eventHandler != null)
     {
         if (timeout >= -1)
         {
             await eventHandler.InvokeAsync(source, arg).TimeoutWrap(timeout, log).ConfigureAwait(false);
         }
         else
         {
             await eventHandler.InvokeAsync(source, arg).ConfigureAwait(false);
         }
     }
 }
Exemple #2
0
        public void InvokeAsyncOfTAggregatesExceptions()
        {
            AsyncEventHandler <EventArgs> handlers = null;

            handlers += (sender, args) =>
            {
                throw new ApplicationException("a");
            };
            handlers += async(sender, args) =>
            {
                await Task.Yield();

                throw new ApplicationException("b");
            };
            var task = handlers.InvokeAsync(null, null);

            try
            {
                task.GetAwaiter().GetResult();
                Assert.True(false, "Expected AggregateException not thrown.");
            }
            catch (AggregateException ex)
            {
                Assert.Equal(2, ex.InnerExceptions.Count);
                Assert.Equal("a", ex.InnerExceptions[0].Message);
                Assert.Equal("b", ex.InnerExceptions[1].Message);
            }
        }
Exemple #3
0
        public void InvokeAsyncOfTNullEverything()
        {
            AsyncEventHandler <EventArgs> handler = null;
            var task = handler.InvokeAsync(null, null);

            Assert.True(task.IsCompleted);
        }
Exemple #4
0
 public static async Task InvokeAsync <TArgs>(this AsyncEventHandler <TArgs> eventHandler, object sender, TArgs args, CancellationToken token)
     where TArgs : EventArgs
 {
     if (!token.IsCancellationRequested)
     {
         await eventHandler.InvokeAsync(sender, args);
     }
 }
Exemple #5
0
        private static void InvokeAsyncOfTHelper(object sender, EventArgs args)
        {
            int invoked = 0;
            AsyncEventHandler <EventArgs> handler = (s, a) =>
            {
                Assert.Same(sender, s);
                Assert.Same(args, a);
                invoked++;
                return(TplExtensions.CompletedTask);
            };
            var task = handler.InvokeAsync(sender, args);

            Assert.True(task.IsCompleted);
            Assert.Equal(1, invoked);
        }
Exemple #6
0
        public void InvokeAsyncOfTExecutesEachHandlerSequentially()
        {
            AsyncEventHandler <EventArgs> handlers = null;
            int counter = 0;

            handlers += async(sender, args) =>
            {
                Assert.Equal(1, ++counter);
                await Task.Yield();

                Assert.Equal(2, ++counter);
            };
            handlers += async(sender, args) =>
            {
                Assert.Equal(3, ++counter);
                await Task.Yield();

                Assert.Equal(4, ++counter);
            };
            var task = handlers.InvokeAsync(null, null);

            task.GetAwaiter().GetResult();
        }
Exemple #7
0
 private async Task AddCommandAsync(int cmdId, AsyncEventHandler handler) => await AddCommandInternalAsync(cmdId, (object sender, EventArgs e) => _joinableTaskFactory.Run(async() => await handler?.InvokeAsync(sender, e)));
Exemple #8
0
 private void AddCommand(int cmdId, AsyncEventHandler handler) =>
 AddCommand(cmdId, (EventHandler)((sender, e) => handler?.InvokeAsync(sender, e)));