예제 #1
0
        public void UpdateText(DocumentFile documentFile, string text)
        {
            if (documentFile.Content.Code == text)
            {
                return;
            }

            var documentId = documentIds[documentFile];

            workspace.UpdateText(documentId, text);
            documentFile.Content.Code = text;
            ResetBuildResult(documentFile);
            updateDiagnosticsAction.InvokeAccumulated();
        }
예제 #2
0
        public void InvokeOnlyIfIdleForDelayTimeWithoutSynchronizationContext()
        {
            Assert.IsNull(SynchronizationContext.Current);

            int actionCallCount = 0;
            var throttledAction = new ThrottledAction(() => Interlocked.Add(ref actionCallCount, 1), ThrottledActionMode.InvokeOnlyIfIdleForDelayTime, TimeSpan.FromMilliseconds(100));

            Assert.IsFalse(throttledAction.IsRunning);

            throttledAction.InvokeAccumulated();
            Assert.IsTrue(throttledAction.IsRunning);
            throttledAction.InvokeAccumulated();
            throttledAction.InvokeAccumulated();

            // Multiple calls of InvokeAccumulated within the delayTime should call the action just once.
            Task.Delay(200).Wait();
            Assert.AreEqual(1, actionCallCount);
            Assert.IsFalse(throttledAction.IsRunning);

            actionCallCount = 0;
            throttledAction.InvokeAccumulated();
            Task.Delay(60).Wait();
            throttledAction.InvokeAccumulated();
            Task.Delay(60).Wait();
            throttledAction.InvokeAccumulated();
            Task.Delay(60).Wait();
            throttledAction.InvokeAccumulated();

            // Calls just once: The waits between InvokeAccumulated are less than the idle (delay) time.
            Task.Delay(200).Wait();
            Assert.AreEqual(1, actionCallCount);
            Assert.IsFalse(throttledAction.IsRunning);
        }
예제 #3
0
 private void ListBoxPreviewDragOver(object sender, DragEventArgs e)
 {
     if (!CanMoveItems(e) && !CanInsertItems(e))
     {
         e.Effects = DragDropEffects.None;
         e.Handled = true;
         return;
     }
     lastPreviewDragOverEventArgs = e;
     throttledAutoScrollAction.InvokeAccumulated();
 }
예제 #4
0
        public void InvokeOnlyIfIdleForDelayTimePerformanceTest()
        {
            int actionCallCount = 0;
            var throttledAction = new ThrottledAction(() => Interlocked.Add(ref actionCallCount, 1), ThrottledActionMode.InvokeOnlyIfIdleForDelayTime, TimeSpan.FromMilliseconds(10));

            for (int i = 0; i < 200000; i++)
            {
                throttledAction.InvokeAccumulated();
            }
            Task.Delay(100).Wait();
            Assert.AreEqual(1, actionCallCount);
        }
예제 #5
0
        private void PositionSliderValueChanged(object sender, RoutedPropertyChangedEventArgs <double> e)
        {
            positionLabel.Text = (string)duratonConverter.Convert(TimeSpan.FromSeconds(e.NewValue), null, null, null);

            if (suppressPositionSliderValueChanged)
            {
                return;
            }

            lastUserSliderValue = e.NewValue;
            throttledSliderValueChangedAction.InvokeAccumulated();
        }
예제 #6
0
        public void InvokeMaxEveryDelayTimeTestWithSynchronizationContext()
        {
            using var context = UnitTestSynchronizationContext.Create();
            int actionCallCount = 0;
            var throttledAction = new ThrottledAction(() => Interlocked.Add(ref actionCallCount, 1), ThrottledActionMode.InvokeMaxEveryDelayTime, TimeSpan.FromMilliseconds(100));

            Assert.IsFalse(throttledAction.IsRunning);

            throttledAction.InvokeAccumulated();
            Assert.IsTrue(throttledAction.IsRunning);
            throttledAction.InvokeAccumulated();
            throttledAction.InvokeAccumulated();

            // As long the unit test synchronization context is not executed the actionCallCount must not be increased.
            Task.Delay(200).Wait();
            Assert.AreEqual(0, actionCallCount);

            // Execute the unit test synchronization context.
            context.WaitFor(() => actionCallCount > 0, TimeSpan.FromMilliseconds(200));
            Assert.AreEqual(1, actionCallCount);
            Assert.IsFalse(throttledAction.IsRunning);
        }
예제 #7
0
        private void WorkspaceChanged(object sender, WorkspaceChangeEventArgs e)
        {
            if (e.Kind == WorkspaceChangeKind.DocumentChanged)
            {
                TaskHelper.Run(async() =>
                {
                    var documentFile = documentIds.SingleOrDefault(x => x.Value == e.DocumentId).Key;
                    if (documentFile == null)
                    {
                        return;
                    }
                    var sourceText            = await GetDocument(documentFile).GetTextAsync();
                    documentFile.Content.Code = sourceText.ToString();

                    if (documentService.ActiveDocumentFile == documentFile)
                    {
                        ResetBuildResult(documentFile);
                        updateDiagnosticsAction.InvokeAccumulated();
                    }
                }, taskScheduler);
            }
        }
        public void Initialize()
        {
            transcodingService.ConvertToMp3AllCommand      = convertToMp3AllCommand;
            transcodingService.ConvertToMp3SelectedCommand = convertToMp3SelectedCommand;
            transcodingService.CancelAllCommand            = cancelAllCommand;
            transcodingService.CancelSelectedCommand       = cancelSelectedCommand;

            shellService.TranscodingListView = new Lazy <object>(InitializeTranscodingListView);

            shellService.Closing += ShellServiceClosing;
            selectionService.MusicFiles.CollectionChanged += (sender, e) => throttledMusicFilesCollectionChangedAction.InvokeAccumulated();
            ((INotifyCollectionChanged)selectionService.SelectedMusicFiles).CollectionChanged += SelectedMusicFilesCollectionChanged;
        }
예제 #9
0
        public void InvokeMaxEveryDelayTimeTestWithoutSynchronizationContext()
        {
            Assert.IsNull(SynchronizationContext.Current);

            int actionCallCount = 0;
            var throttledAction = new ThrottledAction(() => Interlocked.Add(ref actionCallCount, 1), ThrottledActionMode.InvokeMaxEveryDelayTime, TimeSpan.FromMilliseconds(100));

            Assert.IsFalse(throttledAction.IsRunning);

            throttledAction.InvokeAccumulated();
            Assert.IsTrue(throttledAction.IsRunning);
            throttledAction.InvokeAccumulated();
            throttledAction.InvokeAccumulated();

            // Multiple calls of InvokeAccumulated within the delayTime should call the action just once.
            Task.Delay(200).Wait();
            Assert.AreEqual(1, actionCallCount);
            Assert.IsFalse(throttledAction.IsRunning);


            actionCallCount = 0;
            throttledAction.InvokeAccumulated();
            Task.Delay(10).Wait();
            throttledAction.InvokeAccumulated();
            Task.Delay(150).Wait();
            throttledAction.InvokeAccumulated();

            // Calls the action twice: First 2 InvokeAccumulated are within delayTime; Last is after delayTime.
            Task.Delay(200).Wait();
            Assert.AreEqual(2, actionCallCount);
            Assert.IsFalse(throttledAction.IsRunning);


            actionCallCount = 0;
            throttledAction.InvokeAccumulated();
            Task.Delay(10).Wait();
            throttledAction.Cancel();

            // Do not call the action because it is cancelled.
            Task.Delay(200).Wait();
            Assert.AreEqual(0, actionCallCount);
            Assert.IsFalse(throttledAction.IsRunning);

            // Calling Cancel multiple time most not throw an exception
            throttledAction.Cancel();
            throttledAction.Cancel();
        }