11

I am trying to use these two methods (of WP 8) in windows phone 8.1, but it gives error and doesn't compile, most probably becasue they are removed. I tried searching the new APIs but couldn't get any. What are other alternatives for these.

Dispatcher.BeginInvoke( () => {}); msdn link

System.Threading.Thread.Sleep(); msdn link

ac-lap
  • 1,623
  • 2
  • 15
  • 27

4 Answers4

14

They still exists for Windows Phone 8.1 SIlverlight Apps, but not for Windows Phone Store Apps. The replacements for Windows Store Apps is:

Sleep (see Thread.Sleep replacement in .NET for Windows Store):

await System.Threading.Tasks.Task.Delay(TimeSpan.FromSeconds(30));

Dispatcher (see How the Deployment.Current.Dispatcher.BeginInvoke work in windows store app?):

CoreDispatcher dispatcher = CoreWindow.GetForCurrentThread().Dispatcher;
await dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { });
Community
  • 1
  • 1
Johan Falk
  • 4,341
  • 2
  • 30
  • 42
9

Dispatcher.BeginInvoke( () => {}); is replaced by

await this.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async () => {});

and System.Threading.Thread.Sleep(); is replaced by

await Task.Delay(TimeSpan.FromSeconds(doubleValue));
Chris Shao
  • 8,231
  • 3
  • 39
  • 37
6

Be aware that not only has the API changed (adopting the API from WindowsStore apps), but the way that the Dispatcher was obtained in windowsPhone 8.0 has changed as well.

@Johan Faulk's suggestion, although will work, may return null under a multitude of conditions.

Old code to grab the dispatcher:

var dispatcher = Deployment.Current.Dispatcher;
or
Deployment.Current.Dispatcher.BeginInvoke(()=>{
     // any code to modify UI or UI bound elements goes here 
});

New in Windows 8.1 Deployment is not an available object or namespace.

In order to make sure the Main UI Thread dispatcher is obtained, use the following:

var dispatcher = CoreApplication.MainView.CoreWindow.Dispatcher;
or 
CoreApplication.MainWindow.CoreWindow.Dispatcher.RunAsync(
  CoreDispatcherPriority.Normal,
  ()=>{
      // UI code goes here
});

Additionally, although the method SAYS it will be executed Async the keyword await can not be used in the method invoked by RunAsync. (in the above example the method is anonymous).

In order to execute an awaitable method inside anonymous method above, decorate the anonymous method inside RunAsync() with the async keyword.

CoreApplication.MainWindow.CoreWindow.Dispatcher.RunAsync(
CoreDispatcherPriority.Normal,
**async**()=>{
      // UI code goes here
      var response = **await** LongRunningMethodAsync();
});
Zack Weiner
  • 664
  • 4
  • 14
  • Has everything changed again since this post or am I missing something? Your placement of await results in a syntax error, as does my own code when I move "await" from before Dispatcher to before the anonymous method. private async void PlayLaser() { DIspatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, await () => { laser.Play(); }); } (I was trying not to await at all but my audio is sometimes too delayed.) – sraboy Sep 07 '15 at 18:46
  • Please be more specific, Windows 8.1 has not changed since this post. As for Windows 10, this may not be the appropriate thread as it is marked WindowsPhone 8.1. Many of the the APIs have changed completely on WindowsPhone 10 to unify the OS's. – Zack Weiner Sep 08 '15 at 19:57
  • I'm on Windows 10 but building a universal app for 8.1 and Phone 8.1 with VS2015. It's not Win10 Universal. – sraboy Sep 09 '15 at 03:29
  • 1
    Please re-read the last code block of my example along with the notes above it. Your 'troublesome code' places **await** in the place where **async* should be, and does not await the laser.play() which ends up being called synchronously. This is because you've placed await in front of the anonymous method, which is not possible. This should help:---- private async void PlayLaser() { Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async () => { await laser.Play(); }); } – Zack Weiner Sep 09 '15 at 04:49
  • Doh! But Play() isn't awaitable. I thought I was using Dispatcher.RunAsync() to run Play() asynchronously anyway, no? Also, I still get warnings about not having await before Dispatcher.RunAsync() in my async method. Oh well, I guess I've got some reading to do. – sraboy Sep 09 '15 at 07:36
1

For Dispatcher, try this. MSDN

private async Task MyMethod()
{
    await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => { });
}

For Thread.Sleep() try await Task.Delay(1000). MSDN

Farhan Ghumra
  • 15,180
  • 6
  • 50
  • 115