UWP: Ask for Clipboard acces from Background Task

0

I develop an app who paste content to the clipboard from it's background task.

public sealed class ToastBackgroundTask : IBackgroundTask {
    public void Run(IBackgroundTaskInstance taskInstance) {
        //Inside here developer can retrieve and consume the pre-defined 
        //arguments and user inputs;
        var toastArgs = taskInstance.TriggerDetails as ToastNotificationActionTriggerDetail;
        var argument = toastArgs.Argument;
        SetClipbordContent(toastArgs.Argument);
    }
    public static void SetClipbordContent(string text) {
        var dataPackage = new DataPackage();
        dataPackage.SetText(text);
        Clipboard.SetContent(dataPackage);
    }
}

But when I execute the line

Clipboard.SetContent(dataPackage);

raise this exeption:

The activation of a single-threaded class from MTA is not supported.
(Exception from HRESULT: 0x8000001D)

An usual workaround to handle this secario in classic .Net Framework is to use the Thread class to execute this portion of code in a STA context (C# Clipboard.GetText()) But I don't know how to do this in UWP.

c#
.net
windows
uwp
windows-applications
asked on Stack Overflow May 29, 2016 by frank_lbt • edited May 23, 2017 by Community

1 Answer

0

I have used this in the past to execute on the UI thread from a background thread...

await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () => {
    // Do something...
}
answered on Stack Overflow Mar 5, 2020 by Edd

User contributions licensed under CC BY-SA 3.0