public void OnAsyncTaskEnded(object sender, AsyncTaskEndedEventArgs args)
        {
            if (_startTimestampTicks == 0)
            {
                throw new InvalidOperationException("OnAsyncEventStarted was not invoked");
            }

            long endTimestampTicks = _timeSource.GetTimestampTicks();
            long startTimestampUs  = _timeSource.ConvertTicksToUs(_startTimestampTicks);
            long durationUs        = _timeSource.GetDurationUs(_startTimestampTicks, endTimestampTicks);
            int  threadId          = Thread.CurrentThread.ManagedThreadId;

            _tracingLogger.TraceEvent(args.CallerName, EventType.Async, args.CallerType, durationUs,
                                      startTimestampUs, threadId);
            _startTimestampTicks = 0;
        }
Beispiel #2
0
        public Task <T> SubmitAsync <T>(Func <Task <T> > asyncTask, CancellationToken token,
                                        string callerMethodName, Type callerType)
        {
            var completionSource = new TaskCompletionSource <T>();

            token.Register(() => completionSource.TrySetCanceled(token));

            _asyncTasksQueue.Enqueue(async() =>
            {
                OnAsyncTaskStarted?.Invoke(this, EventArgs.Empty);
                try
                {
                    if (!token.IsCancellationRequested)
                    {
                        _insideAsync.Value = true;
                        _currentOperationCancellationToken = token;
                        completionSource.TrySetResult(await asyncTask());
                    }
                }
                catch (Exception e)
                {
                    completionSource.TrySetException(e);
                }
                finally
                {
                    var eventArgs = new AsyncTaskEndedEventArgs
                    {
                        CallerName = callerMethodName, CallerType = callerType
                    };
                    OnAsyncTaskEnded?.Invoke(this, eventArgs);
                    _currentOperationCancellationToken = CancellationToken.None;
                    _insideAsync.Value = false;
                }
            });
            return(completionSource.Task);
        }