示例#1
0
        private void ButtonStart_Click(object sender, RoutedEventArgs e)
        {
            txtResult.Text     = "";
            btnStop.IsEnabled  = true;
            btnStart.IsEnabled = false;
            _controller        = DispatcherQueueController.CreateOnDedicatedThread();

            _controller.DispatcherQueue.TryEnqueue(() =>
            {
                var primeNumbers = new List <int>();
                for (int i = 1; i <= 100000; i++)
                {
                    var count = 0;
                    for (int j = 2; j < i; j++)
                    {
                        if ((i % j) == 0)
                        {
                            count++;
                        }
                    }
                    if (count == 0)
                    {
                        primeNumbers.Add(i);
                    }

                    UpdateProgressBar(i / 1000);
                }

                UpateTextBlock(primeNumbers);
            });
        }
示例#2
0
        public static DispatcherQueueController CreateDispatcherQueueControllerForCurrentThread()
        {
            var options = new DispatcherQueueOptions
            {
                dwSize        = Marshal.SizeOf <DispatcherQueueOptions>(),
                threadType    = DISPATCHERQUEUE_THREAD_TYPE.DQTYPE_THREAD_CURRENT,
                apartmentType = DISPATCHERQUEUE_THREAD_APARTMENTTYPE.DQTAT_COM_NONE
            };

            DispatcherQueueController controller = null;
            uint hr = CreateDispatcherQueueController(options, out IntPtr controllerPointer);

            if (hr == 0)
            {
                controller = Marshal.GetObjectForIUnknown(controllerPointer) as DispatcherQueueController;
                Marshal.Release(controllerPointer);
            }

            return(controller);
        }
示例#3
0
        public Capture(CanvasDevice device, GraphicsCaptureItem item)
        {
            _item   = item;
            _device = device;

            // TODO: Dpi?
            _swapChain = new CanvasSwapChain(_device, item.Size.Width, item.Size.Height, 96);

            _dispatcherQueueController = DispatcherQueueController.CreateOnDedicatedThread();
            _dispatcherQueue           = _dispatcherQueueController.DispatcherQueue;

            // We don't want to return from the constructor untill the frame pool and
            // the capture session are both initialized. We could do this on the UI thread,
            // but you really shouldn't. This will update as fast as the screen refresh rate,
            // so it would cause performance issues on the UI thread.
            var wait    = new AutoResetEvent(false);
            var success = _dispatcherQueue.TryEnqueue(() =>
            {
                _framePool = Direct3D11CaptureFramePool.Create(
                    _device,
                    DirectXPixelFormat.B8G8R8A8UIntNormalized,
                    2,
                    item.Size);
                _session  = _framePool.CreateCaptureSession(item);
                _lastSize = item.Size;

                _framePool.FrameArrived += OnFrameArrived;

                wait.Set();
            });

            if (!success)
            {
                throw new Exception("Could not enqueue work!");
            }

            wait.WaitOne();
        }