Skip to content

Commit

Permalink
Merge pull request #5 from gemstone/BackgroundToInit
Browse files Browse the repository at this point in the history
Change `IsBackground` property of `LongSynchronizedOperation` to use `init`
  • Loading branch information
StephenCWills authored Dec 14, 2023
2 parents 9008df4 + a1a57fc commit 48166cf
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ namespace Gemstone.Threading.SynchronizedOperations;
/// Represents a long-running synchronized operation that cannot run while it is already in progress.
/// </summary>
/// <remarks>
/// <para>
/// The action performed by the <see cref="LongSynchronizedOperation"/> is executed on
/// its own dedicated thread when running the operation in the foreground asynchronously.
/// When running on its own thread, the action is executed in a tight loop until all
Expand All @@ -40,11 +39,6 @@ namespace Gemstone.Threading.SynchronizedOperations;
/// It is also recommended to prefer this type of operation if the speed of the operation
/// is not critical or if completion of the operation is critical, such as when saving data
/// to a file.
/// </para>
/// <para>
/// If the <see cref="IsBackground"/> property is changed while the synchronized operation
/// is running, behavior is undefined.
/// </para>
/// </remarks>
public class LongSynchronizedOperation : SynchronizedOperationBase
{
Expand Down Expand Up @@ -105,7 +99,11 @@ public LongSynchronizedOperation(Action<CancellationToken> action, Action<Except
/// shutdown; consider a cancellable action for <see cref="LongSynchronizedOperation"/>
/// instances that use a foreground thread.
/// </remarks>
#if NET
public bool IsBackground { get; init; }
#else
public bool IsBackground { get; set; }
#endif

#endregion

Expand All @@ -124,25 +122,25 @@ protected override void ExecuteActionAsync()

private void ExecuteActionAsyncBackground()
{
void taskAction()
void TaskAction()
{
if (ExecuteAction())
ExecuteActionAsync();
}

Task.Factory.StartNew(taskAction, TaskCreationOptions.LongRunning);
Task.Factory.StartNew(TaskAction, TaskCreationOptions.LongRunning);
}

private void ExecuteActionAsyncForeground()
{
void threadAction()
void ThreadAction()
{
while (ExecuteAction())
{
}
}

new Thread(threadAction).Start();
new Thread(ThreadAction).Start();
}

#endregion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,13 @@ public class TaskSynchronizedOperation : ISynchronizedOperation
private class SynchronizedOperation : SynchronizedOperationBase
{
private Func<CancellationToken, Task> AsyncAction { get; }

private Action<Exception>? ExceptionHandler { get; }

public SynchronizedOperation(Func<CancellationToken, Task> asyncAction, Action<Exception>? exceptionHandler)
: base(() => { })
{
if (asyncAction is null)
throw new ArgumentNullException(nameof(asyncAction));

AsyncAction = asyncAction;
AsyncAction = asyncAction ?? throw new ArgumentNullException(nameof(asyncAction));
ExceptionHandler = exceptionHandler;
}

Expand Down Expand Up @@ -157,7 +155,7 @@ public TaskSynchronizedOperation(Func<CancellationToken, Task> asyncAction, Acti
public bool IsRunning => InternalSynchronizedOperation.IsRunning;

/// <summary>
/// Gets a value to indiate whether the synchronized operation
/// Gets a value to indicate whether the synchronized operation
/// has an additional operation that is pending execution after
/// the currently running action has completed.
/// </summary>
Expand Down

0 comments on commit 48166cf

Please sign in to comment.