| 1 |
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information. |
| 2 |
|
| 3 |
using System; |
| 4 |
using System.Reactive.Disposables; |
| 5 |
|
| 6 |
namespace System.Reactive.Concurrency |
| 7 |
{ |
| 8 |
public static partial class Scheduler |
| 9 |
{ |
| 10 |
/// <summary> |
| 11 |
/// Schedules an action to be executed. |
| 12 |
/// </summary> |
| 13 |
/// <param name="scheduler">Scheduler to execute the action on.</param> |
| 14 |
/// <param name="action">Action to execute.</param> |
| 15 |
/// <returns>The disposable object used to cancel the scheduled action (best effort).</returns> |
| 16 |
/// <exception cref="ArgumentNullException"><paramref name="scheduler"/> or <paramref name="action"/> is null.</exception> |
| 17 |
public static IDisposable Schedule(this IScheduler scheduler, Action action) |
| 18 |
{ |
| 19 |
if (scheduler == null) |
| 20 |
throw new ArgumentNullException("scheduler"); |
| 21 |
if (action == null) |
| 22 |
throw new ArgumentNullException("action"); |
| 23 |
|
| 24 |
return scheduler.Schedule(action, Invoke); |
| 25 |
} |
| 26 |
|
| 27 |
/// <summary> |
| 28 |
/// Schedules an action to be executed after the specified relative due time. |
| 29 |
/// </summary> |
| 30 |
/// <param name="scheduler">Scheduler to execute the action on.</param> |
| 31 |
/// <param name="action">Action to execute.</param> |
| 32 |
/// <param name="dueTime">Relative time after which to execute the action.</param> |
| 33 |
/// <returns>The disposable object used to cancel the scheduled action (best effort).</returns> |
| 34 |
/// <exception cref="ArgumentNullException"><paramref name="scheduler"/> or <paramref name="action"/> is null.</exception> |
| 35 |
public static IDisposable Schedule(this IScheduler scheduler, TimeSpan dueTime, Action action) |
| 36 |
{ |
| 37 |
if (scheduler == null) |
| 38 |
throw new ArgumentNullException("scheduler"); |
| 39 |
if (action == null) |
| 40 |
throw new ArgumentNullException("action"); |
| 41 |
|
| 42 |
return scheduler.Schedule(action, dueTime, Invoke); |
| 43 |
} |
| 44 |
|
| 45 |
/// <summary> |
| 46 |
/// Schedules an action to be executed at the specified absolute due time. |
| 47 |
/// </summary> |
| 48 |
/// <param name="scheduler">Scheduler to execute the action on.</param> |
| 49 |
/// <param name="action">Action to execute.</param> |
| 50 |
/// <param name="dueTime">Absolute time at which to execute the action.</param> |
| 51 |
/// <returns>The disposable object used to cancel the scheduled action (best effort).</returns> |
| 52 |
/// <exception cref="ArgumentNullException"><paramref name="scheduler"/> or <paramref name="action"/> is null.</exception> |
| 53 |
public static IDisposable Schedule(this IScheduler scheduler, DateTimeOffset dueTime, Action action) |
| 54 |
{ |
| 55 |
if (scheduler == null) |
| 56 |
throw new ArgumentNullException("scheduler"); |
| 57 |
if (action == null) |
| 58 |
throw new ArgumentNullException("action"); |
| 59 |
|
| 60 |
return scheduler.Schedule(action, dueTime, Invoke); |
| 61 |
} |
| 62 |
|
| 63 |
/// <summary> |
| 64 |
/// Schedules an action to be executed. |
| 65 |
/// </summary> |
| 66 |
/// <param name="scheduler">Scheduler to execute the action on.</param> |
| 67 |
/// <param name="action">Action to execute.</param> |
| 68 |
/// <returns>The disposable object used to cancel the scheduled action (best effort).</returns> |
| 69 |
/// <exception cref="ArgumentNullException"><paramref name="scheduler"/> or <paramref name="action"/> is null.</exception> |
| 70 |
public static IDisposable ScheduleLongRunning(this ISchedulerLongRunning scheduler, Action<ICancelable> action) |
| 71 |
{ |
| 72 |
if (scheduler == null) |
| 73 |
throw new ArgumentNullException("scheduler"); |
| 74 |
if (action == null) |
| 75 |
throw new ArgumentNullException("action"); |
| 76 |
|
| 77 |
return scheduler.ScheduleLongRunning(action, (a, c) => a(c)); |
| 78 |
} |
| 79 |
|
| 80 |
static IDisposable Invoke(IScheduler scheduler, Action action) |
| 81 |
{ |
| 82 |
action(); |
| 83 |
return Disposable.Empty; |
| 84 |
} |
| 85 |
} |
| 86 |
} |