How to call StartDragAsync from CoreIndependentInputSource Pointer event UWP

96 views Asked by At

I have a UWP Application where CoreIndependentInputSource is used to get pointer events

As its runs on Separate Thread and so PointerPressed/Moved/Relased also triggered on separate thread

    coreInput = swapChainPanel->CreateCoreIndependentInputSource(
            CoreInputDeviceTypes::Mouse |
            CoreInputDeviceTypes::Touch |
            CoreInputDeviceTypes::Pen
            );

        // Register for pointer events, which will be raised on the dedicated input thread.
        coreInput->PointerPressed += ref new TypedEventHandler<Object^, PointerEventArgs^>(this, &Scenario1_LowLatencyInput::OnPointerPressed);
        .
        .
        coreInput->Dispatcher->ProcessEvents(CoreProcessEventsOption::ProcessUntilQuit);
    }); 
inputLoopWorker = ThreadPool::RunAsync(inputWorkItemHandler, WorkItemPriority::High, WorkItemOptions::TimeSliced);

UWP StartDragAsync function needs PointerPoint as parameter But this function need to be called from UI thread.

Unfortunately PointerPoint Object has thread affinity. So as its created in Input Dispatcher Thread it can not be passed to UI thread

void Scenario1_LowLatencyInput::OnPointerPressed(Object^ sender, PointerEventArgs^ args)
{
    // Event Received in Input Dispatcher Thread 
    PointerPoint^ currentPoint = args->CurrentPoint;

    this->Dispatcher->RunAsync(Windows::UI::Core::CoreDispatcherPriority::Normal,
              ref new Windows::UI::Core::DispatchedHandler([this, currentPoint]()
    {
        // Have to run on UI thread for StartDragAsync
        DragUIItem->StartDragAsync(currentPoint); //!!!! Exception will occur here because currentPoint is not created in UI thread

    }));
  • If I call DragUIItem->StartDragAsync in InputDispatcher Thread then Exception occurs because StartDragAsync need to be called from UI thread.

  • If I pass this point to DragUIItem->StartDragAsync in UI thread then Exception occurs because currentPoint created in InputDispatcher thread so StartDragAsync Function (which is now executing in UI thread) can not use it

Does any knows how to overcome this problem.

0

There are 0 answers