forked from MarimerLLC/csla
-
Notifications
You must be signed in to change notification settings - Fork 3
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 TaskExtensions and tests; use in HttpProxy
- Loading branch information
1 parent
9576666
commit e5f3998
Showing
7 changed files
with
168 additions
and
47 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
This file was deleted.
Oops, something went wrong.
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,99 @@ | ||
//----------------------------------------------------------------------- | ||
// <copyright file="TaskExtensions.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 TaskExtensions | ||
{ | ||
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> | ||
/// <remarks> | ||
/// The async function is run on a thread in the thread | ||
/// pool, the result is marshalled back to the calling | ||
/// thread. The calling thread's context is carried | ||
/// onto the background thread. | ||
/// </remarks> | ||
/// <param name="task">Task to run synchronously.</param> | ||
public static T RunWithContext<T>(this Task<T> task) | ||
{ | ||
var context = new ContextParams(); | ||
var background = Task.Run(() => | ||
{ | ||
context.SetThreadContext(); | ||
task.RunSynchronously(); | ||
}); | ||
SpinWait(background); | ||
return task.Result; | ||
} | ||
|
||
/// <summary> | ||
/// Run an async function synchronously without | ||
/// blocking the UI thread. | ||
/// </summary> | ||
/// <remarks> | ||
/// The async function is run on a thread in the thread | ||
/// pool, the result is marshalled back to the calling | ||
/// thread. The calling thread's context is carried | ||
/// onto the background thread. | ||
/// </remarks> | ||
/// <param name="task">Task to run synchronously.</param> | ||
/// <param name="timeout">Max time to wait for completion.</param> | ||
public static T RunWithContext<T>(this Task<T> task, TimeSpan timeout) | ||
{ | ||
var context = new ContextParams(); | ||
var background = Task.Run(() => | ||
{ | ||
context.SetThreadContext(); | ||
task.RunSynchronously(); | ||
}); | ||
SpinWait(background, timeout); | ||
return task.Result; | ||
} | ||
|
||
/// <summary> | ||
/// Wait synchronously (spinwait) for the task to complete. | ||
/// </summary> | ||
/// <param name="task">Task on which to wait.</param> | ||
public static void SpinWait(this Task task) | ||
{ | ||
while (!task.IsCompleted) | ||
{ | ||
Thread.Sleep(1); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Wait synchronously (spinwait) for the task to complete. | ||
/// </summary> | ||
/// <param name="task">Task on which to wait.</param> | ||
/// <param name="timeout">Timeout value</param> | ||
public static void SpinWait(this Task task, TimeSpan timeout) | ||
{ | ||
var deadline = DateTime.Now + timeout; | ||
while (!task.IsCompleted) | ||
{ | ||
if (DateTime.Now > deadline) | ||
throw new TimeoutException(); | ||
Thread.Sleep(1); | ||
} | ||
} | ||
} | ||
} |
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,62 @@ | ||
//----------------------------------------------------------------------- | ||
// <copyright file="TaskExtenstionTests.cs" company="Marimer LLC"> | ||
// Copyright (c) Marimer LLC. All rights reserved. | ||
// Website: http://www.lhotka.net/cslanet/ | ||
// </copyright> | ||
// <summary>no summary</summary> | ||
//----------------------------------------------------------------------- | ||
using System; | ||
using System.Threading.Tasks; | ||
|
||
#if !NUNIT | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
#else | ||
using NUnit.Framework; | ||
using TestClass = NUnit.Framework.TestFixtureAttribute; | ||
using TestInitialize = NUnit.Framework.SetUpAttribute; | ||
using TestCleanup = NUnit.Framework.TearDownAttribute; | ||
using TestMethod = NUnit.Framework.TestAttribute; | ||
#endif | ||
using Csla.Threading; | ||
|
||
namespace Csla.Test.Threading | ||
{ | ||
[TestClass] | ||
public class TaskExtenstionTests | ||
{ | ||
[TestMethod] | ||
public void SpinWait() | ||
{ | ||
int x = 0; | ||
var task = Task.Run(() => { System.Threading.Thread.Sleep(10); x = 42; }); | ||
task.SpinWait(); | ||
Assert.AreEqual(42, x); | ||
} | ||
|
||
[TestMethod] | ||
[ExpectedException(typeof(TimeoutException))] | ||
public void SpinWaitTimeout() | ||
{ | ||
var task = Task.Run(() => System.Threading.Thread.Sleep(10)); | ||
task.SpinWait(new TimeSpan(0, 0, 0)); | ||
} | ||
|
||
[TestMethod] | ||
public void RunWithContext() | ||
{ | ||
int x = 0; | ||
ApplicationContext.ClientContext["x"] = 42; | ||
var task = new Task<int>(() => (int)ApplicationContext.ClientContext["x"]); | ||
x = task.RunWithContext(); | ||
Assert.AreEqual(42, x); | ||
} | ||
|
||
[TestMethod] | ||
[ExpectedException(typeof(TimeoutException))] | ||
public void RunWithContextTimeout() | ||
{ | ||
var task = new Task<int>(() => { System.Threading.Thread.Sleep(10); return 42; }); | ||
int x = task.RunWithContext(new TimeSpan(0, 0, 0)); | ||
} | ||
} | ||
} |
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