-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b4118aa
commit 28a2fac
Showing
3 changed files
with
122 additions
and
120 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
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(); | ||
} | ||
} |
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 @@ | ||
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(); | ||
} | ||
} |