Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
nameofSEOKWONHONG committed Oct 10, 2024
1 parent b4118aa commit 28a2fac
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 120 deletions.
120 changes: 0 additions & 120 deletions src/Job/JobHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,124 +25,4 @@ public T Dequeue()

return null;
}
}

public class JobProcessor<T> : IDisposable
where T : class
{
private JobHandler<T> _handler;
private Action<T> _action;
private readonly CancellationTokenSource _cts = new CancellationTokenSource();

private Thread _thread;
public JobProcessor()
{
_thread = new Thread(Start);
_thread.IsBackground = true;
_thread.Start();
}

public void SetProcess(JobHandler<T> jobHandler, Action<T> callback)
{
_handler = jobHandler;
_action = callback;
}

private void Start()
{
while (!_cts.Token.IsCancellationRequested)
{
try
{
if (_handler.xIsNotEmpty())
{
if (_action.xIsNotEmpty())
{
var item = _handler.Dequeue();
if (item.xIsNotNull())
{
_action(item);
}
}
}
Thread.Sleep(10);
}
catch (InvalidOperationException e)
{
Console.WriteLine(e);
break;
}
}
}

public void Stop()
{
_cts.Cancel();
}

public void Dispose()
{
_cts?.Cancel();
}
}

public class JobProcessorAsync<T> : IDisposable
where T : class
{
private JobHandler<T> _handler;
private Func<T, Task> _func;
private readonly CancellationTokenSource _cts = new CancellationTokenSource();

private Task _task;

public JobProcessorAsync()
{
_task = Task.Run(Start);
}

public void SetProcess(JobHandler<T> jobHandler, Func<T, Task> callback)
{
_handler = jobHandler;
_func = callback;
}

private async Task Start()
{
while (!_cts.Token.IsCancellationRequested)
{
try
{
if (_handler.xIsNotEmpty())
{
if (_func.xIsNotEmpty())
{
var item = _handler.Dequeue();
if (item.xIsNotNull())
{
await _func(item);
}
}
}
// Instead of Thread.Sleep, we use Task.Delay to make this asynchronous
await Task.Delay(10);
}
catch (Exception e)
{
Console.WriteLine(e);
break;
}
}
}

public void Stop()
{
_cts.Cancel();
_task.Wait(); // Wait for the task to finish gracefully
}

public void Dispose()
{
_cts?.Cancel();
_task?.Wait();
}
}
60 changes: 60 additions & 0 deletions src/Job/JobProcessor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
namespace eXtensionSharp.Job;

public class JobProcessor<T> : IDisposable
where T : class
{
private JobHandler<T> _handler;
private Action<T> _action;
private readonly CancellationTokenSource _cts = new CancellationTokenSource();

private Thread _thread;
public JobProcessor()
{
_thread = new Thread(Start);
_thread.IsBackground = true;
_thread.Start();
}

public void SetProcess(JobHandler<T> jobHandler, Action<T> callback)
{
_handler = jobHandler;
_action = callback;
}

private void Start()
{
while (!_cts.Token.IsCancellationRequested)
{
try
{
if (_handler.xIsNotEmpty())
{
if (_action.xIsNotEmpty())
{
var item = _handler.Dequeue();
if (item.xIsNotNull())
{
_action(item);
}
}
}
Thread.Sleep(10);
}
catch (InvalidOperationException e)
{
Console.WriteLine(e);
break;
}
}
}

public void Stop()
{
_cts.Cancel();
}

public void Dispose()
{
_cts?.Cancel();
}
}
62 changes: 62 additions & 0 deletions src/Job/JobProcessorAsync.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
namespace eXtensionSharp.Job;

public class JobProcessorAsync<T> : IDisposable
where T : class
{
private JobHandler<T> _handler;
private Func<T, Task> _func;
private readonly CancellationTokenSource _cts = new CancellationTokenSource();

private Task _task;

public JobProcessorAsync()
{
_task = Task.Run(Start);
}

public void SetProcess(JobHandler<T> jobHandler, Func<T, Task> callback)
{
_handler = jobHandler;
_func = callback;
}

private async Task Start()
{
while (!_cts.Token.IsCancellationRequested)
{
try
{
if (_handler.xIsNotEmpty())
{
if (_func.xIsNotEmpty())
{
var item = _handler.Dequeue();
if (item.xIsNotNull())
{
await _func(item);
}
}
}
// Instead of Thread.Sleep, we use Task.Delay to make this asynchronous
await Task.Delay(10);
}
catch (Exception e)
{
Console.WriteLine(e);
break;
}
}
}

public void Stop()
{
_cts.Cancel();
_task.Wait(); // Wait for the task to finish gracefully
}

public void Dispose()
{
_cts?.Cancel();
_task?.Wait();
}
}

0 comments on commit 28a2fac

Please sign in to comment.