0

I have the background task, and I want to show a message dialog in this background task in some situations like below.

public sealed class TestTask : IBackgroundTask
{
   public async void Run(IBackgroundTaskInstance taskInstance)
   {
      dispatcher = CoreWindow.GetForCurrentThread().Dispatcher;  // -> Crash this line
   }
}

As you can see from Problems with show dialog on Background task in windows phone 8.1 when I trying to access the UI thread from background task I want to use CoreDispatcher. But when I want to use CoreDispatcher in UWP background task, CoreWindows.GetForCurrentThread().Dispatcher is null in constructor and run method, and I get null pointer exception with below message

Object reference not set to an instance of an object

Is something has changed in windows phone 10 (UWP) from Windows phone 8.1?

Dharman
  • 30,962
  • 25
  • 85
  • 135
sorosh_sabz
  • 2,356
  • 2
  • 32
  • 53
  • You have no window when you are running on the background task, hence name BackgroundTask... Please read documentation... – Barptad Nov 29 '16 at 22:33
  • @Barptad Ok, so how to show message dialog in background task? – sorosh_sabz Nov 29 '16 at 22:34
  • @Barptad It is required in some case to show some message to users when some event occur in background task. – sorosh_sabz Nov 29 '16 at 22:37
  • 1
    Show toast... background tasks are intended for long running actions not for user interactions... – Barptad Nov 29 '16 at 22:38
  • @Barptad toast does not have any button to get some response from user, but MessageDialog has UICommand that can be used for get some response from user. I need something like this. – sorosh_sabz Nov 29 '16 at 23:00
  • 3
    @CodyGray [ITNOA](https://en.wikipedia.org/wiki/Basmala). Going back to the original question: Windows 10 adds [interactive toasts](https://blogs.msdn.microsoft.com/tiles_and_toasts/2015/07/02/adaptive-and-interactive-toast-notifications-for-windows-10/). – Raymond Chen Nov 30 '16 at 00:14
  • Why down vote this question? Is there any problem in question that cause to down vote? When problems are not told how a better question to be asked in the future? – sorosh_sabz Nov 30 '16 at 07:08
  • @RaymondChen very thx to reply this question, but is there any way to force end user to could not skip toast. in another word I want to force end user to answer the popup question anyway. – sorosh_sabz Nov 30 '16 at 07:09
  • That would be a denial of service. E.g. ransomware could just keep displaying unskippable toasts until you pay the money. – Raymond Chen Nov 30 '16 at 13:52

1 Answers1

1

Just to clear up this question, based on the comments here are your options.

  1. If you want to have a background task which needs to show information with additional actions to take upon completion use interactive toasts.
  2. If this user interaction action is critical use modals in the actual app and look at in process background tasks, however I see no reason why the need for background task at that point.

If none of the above options suits you it's time to reconsider your current scenario...

Barptad
  • 174
  • 1
  • 8