Skip to content

Commit

Permalink
Schedule
Browse files Browse the repository at this point in the history
  • Loading branch information
artemiusgreat committed Feb 25, 2024
1 parent fa32938 commit 31c194d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 22 deletions.
2 changes: 1 addition & 1 deletion Canvas.Core/Canvas.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<ImplicitUsings>disable</ImplicitUsings>
<Nullable>disable</Nullable>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<Version>1.9.8</Version>
<Version>1.9.9</Version>
<Description>Internal package used in Canvas.Views.Web</Description>
<Authors>artemiusgreat</Authors>
<Copyright>indemos.com</Copyright>
Expand Down
42 changes: 22 additions & 20 deletions Canvas.Core/Services/ScheduleService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,18 @@ namespace Canvas.Core.Services
{
public class ScheduleService : IDisposable
{
protected virtual Task Consumer { get; set; }
protected virtual BlockingCollection<(Action, TaskCompletionSource)> Queue { get; set; }
protected virtual CancellationTokenSource Cancellation { get; set; }
protected virtual BlockingCollection<(Action, TaskCompletionSource)> Queue { get; set; }

/// <summary>
/// Dispose
/// Constructor
/// </summary>
public virtual void Dispose()
public ScheduleService()
{
Queue?.Dispose();
Cancellation?.Cancel();
}
Queue = new();
Cancellation = new CancellationTokenSource();

/// <summary>
/// Schedule
/// </summary>
/// <param name="action"></param>
/// <returns></returns>
public virtual Task Schedule(Action action)
{
Queue ??= new();
Cancellation ??= new CancellationTokenSource();
Consumer ??= Task.Factory.StartNew(() =>
Task.Factory.StartNew(() =>
{
foreach (var (o, com) in Queue.GetConsumingEnumerable())
{
Expand All @@ -39,15 +28,28 @@ public virtual Task Schedule(Action action)
},
Cancellation.Token,
TaskCreationOptions.LongRunning,
TaskScheduler.Current).ContinueWith(o => Consumer.Dispose());
TaskScheduler.Current).ContinueWith(o => Queue.Dispose());
}

/// <summary>
/// Dispose
/// </summary>
public virtual void Dispose() => Cancellation?.Cancel();

/// <summary>
/// Schedule
/// </summary>
/// <param name="action"></param>
/// <returns></returns>
public virtual Task Schedule(Action action)
{
var completion = new TaskCompletionSource();

if (Queue.Count > 1)
{
Queue.TryTake(out var o);
}

var completion = new TaskCompletionSource();

Queue.TryAdd((action, completion));

return completion.Task;
Expand Down
2 changes: 1 addition & 1 deletion Canvas.Views.Web/Canvas.Views.Web.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<PropertyGroup>
<IsPackable>true</IsPackable>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<Version>1.9.8</Version>
<Version>1.9.9</Version>
<PackageTags>finance chart opengl canvas trading gdi stock direct2d</PackageTags>
<Authors>artemiusgreat</Authors>
<Copyright>indemos.com</Copyright>
Expand Down

0 comments on commit 31c194d

Please sign in to comment.