forked from MarimerLLC/csla
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MarimerLLC#1059 Add SyncTask and use in HttpProxy for sync operations
- Loading branch information
1 parent
bb1ce66
commit 23904c9
Showing
3 changed files
with
53 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
//----------------------------------------------------------------------- | ||
// <copyright file="SyncTask.cs" company="Marimer LLC"> | ||
// Copyright (c) Marimer LLC. All rights reserved. | ||
// Website: http://www.lhotka.net/cslanet/ | ||
// </copyright> | ||
// <summary></summary> | ||
//----------------------------------------------------------------------- | ||
using System; | ||
using System.Globalization; | ||
using System.Security.Principal; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace Csla.Threading | ||
{ | ||
/// <summary> | ||
/// Run an async operation synchronously without | ||
/// blocking the UI thread. | ||
/// </summary> | ||
public static class SyncTask | ||
{ | ||
private static readonly TaskFactory _myTaskFactory = new TaskFactory(CancellationToken.None, TaskCreationOptions.None, TaskContinuationOptions.None, TaskScheduler.Default); | ||
|
||
/// <summary> | ||
/// Run an async function synchronously without | ||
/// blocking the UI thread. | ||
/// </summary> | ||
public static T Run<T>(Func<Task<T>> func) | ||
{ | ||
var context = new ContextParams(); | ||
return _myTaskFactory.StartNew<Task<T>>(delegate | ||
{ | ||
context.SetThreadContext(); | ||
return func(); | ||
}).Unwrap<T>().GetAwaiter().GetResult(); | ||
} | ||
} | ||
} |