Skip to content

Commit

Permalink
Added a "BeginInvokeOnMainThreadAsync" method to "IDeviceService"
Browse files Browse the repository at this point in the history
  • Loading branch information
rssllgrrtt committed Apr 25, 2020
1 parent 8597347 commit 94155bb
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/Forms/Prism.Forms/Services/DeviceService.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Prism.AppModel;
using System;
using System.Threading.Tasks;
using Xamarin.Forms;

namespace Prism.Services
Expand Down Expand Up @@ -66,6 +67,30 @@ public void BeginInvokeOnMainThread(Action action)
Device.BeginInvokeOnMainThread(action);
}

/// <summary>
/// Invokes an action (which can be awaited) on the device main UI thread.
/// </summary>
/// <param name="action">The Action to invoke</param>
public Task BeginInvokeOnMainThreadAsync(Action action)
{
var tcs = new TaskCompletionSource<bool>();

Device.BeginInvokeOnMainThread(() =>
{
try
{
action();
tcs.TrySetResult(true);
}
catch (Exception ex)
{
tcs.TrySetException(ex);
}
});

return tcs.Task;
}

/// <summary>
/// Starts a recurring timer using the Device clock capabilities.
/// </summary>
Expand Down
7 changes: 7 additions & 0 deletions src/Forms/Prism.Forms/Services/IDeviceService.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Prism.AppModel;
using System;
using System.Threading.Tasks;
using Xamarin.Forms;

namespace Prism.Services
Expand Down Expand Up @@ -29,6 +30,12 @@ public interface IDeviceService
/// </summary>
/// <param name="action">The Action to invoke</param>
void BeginInvokeOnMainThread(Action action);

/// <summary>
/// Invokes an action (which can be awaited) on the device main UI thread.
/// </summary>
/// <param name="action">The Action to invoke</param>
Task BeginInvokeOnMainThreadAsync(Action action);

/// <summary>
/// Starts a recurring timer using the Device clock capabilities.
Expand Down

0 comments on commit 94155bb

Please sign in to comment.