UltraWelfare
UltraWelfare
CC#
Created by UltraWelfare on 7/17/2024 in #help
✅ Waiting on a task more efficiently
No description
8 replies
CC#
Created by UltraWelfare on 7/17/2024 in #help
✅ Waiting on a task more efficiently
$close
8 replies
CC#
Created by UltraWelfare on 7/17/2024 in #help
✅ Waiting on a task more efficiently
Actually I thought of using a semaphore to do it:
public void EnqueueJob(IcsCashDesk cashDesk)
{
_jobQueue.Enqueue(cashDesk);
_signal.Release();
}

private async Task ProcessJobs()
{
while (!_cts.IsCancellationRequested)
{
try
{
await _signal.WaitAsync(_cts.Token);
if (_jobQueue.IsEmpty) continue;
while (_jobQueue.TryDequeue(out var cashDesk))
{
await cashDesk.CompleteWaitAsync();
}
}
catch (OperationCanceledException)
{
}
}
}
public void EnqueueJob(IcsCashDesk cashDesk)
{
_jobQueue.Enqueue(cashDesk);
_signal.Release();
}

private async Task ProcessJobs()
{
while (!_cts.IsCancellationRequested)
{
try
{
await _signal.WaitAsync(_cts.Token);
if (_jobQueue.IsEmpty) continue;
while (_jobQueue.TryDequeue(out var cashDesk))
{
await cashDesk.CompleteWaitAsync();
}
}
catch (OperationCanceledException)
{
}
}
}
8 replies
CC#
Created by UltraWelfare on 2/2/2024 in #help
Blazor Server not responding
I ended up turning debug logging on config and finding out I didn't call some host functions in the correct order
4 replies
CC#
Created by UltraWelfare on 2/2/2024 in #help
Blazor Server not responding
It's too many lines to be compared...
4 replies
CC#
Created by UltraWelfare on 1/25/2024 in #help
Primary constructor parameter is null after being run in a Task
What do you mean
12 replies
CC#
Created by UltraWelfare on 1/25/2024 in #help
Primary constructor parameter is null after being run in a Task
If you debug inside the lambda or DoWork the printService would be shown as null (although it isn't)
12 replies
CC#
Created by UltraWelfare on 1/25/2024 in #help
Primary constructor parameter is null after being run in a Task
It's a recreation of the debugger bug
12 replies
CC#
Created by UltraWelfare on 1/25/2024 in #help
Primary constructor parameter is null after being run in a Task
You can take a look at this small snippet:
internal class Program
{
static void Main(string[] args)
{
var printService = new PrintService();
var testService = new TestService(printService);

testService.StartExecuting();
Thread.Sleep(5000);
testService.StopExecuting();
Console.WriteLine("Done");
}
}

class PrintService
{
public void PrintSomething()
{
Console.WriteLine("Something");
}
}

class TestService(PrintService printService)
{

private CancellationTokenSource _cts;
private Task _task;

public void StartExecuting(CancellationToken cancellationToken = default)
{
_cts = new();
_task = Task.Run(async () =>
{
await DoWork(_cts.Token);
}, cancellationToken);
}

public void StopExecuting(CancellationToken cancellationToken = default)
{
_cts.Cancel();
_task.Wait(cancellationToken);
}

public async Task DoWork(CancellationToken cancellationToken)
{
while (true)
{
try
{
Console.WriteLine("Doing work");
printService.PrintSomething();

await Task.Delay(1000, cancellationToken);
} catch (OperationCanceledException)
{
Console.WriteLine("Operation cancelled");
break;
}
}
}
}
internal class Program
{
static void Main(string[] args)
{
var printService = new PrintService();
var testService = new TestService(printService);

testService.StartExecuting();
Thread.Sleep(5000);
testService.StopExecuting();
Console.WriteLine("Done");
}
}

class PrintService
{
public void PrintSomething()
{
Console.WriteLine("Something");
}
}

class TestService(PrintService printService)
{

private CancellationTokenSource _cts;
private Task _task;

public void StartExecuting(CancellationToken cancellationToken = default)
{
_cts = new();
_task = Task.Run(async () =>
{
await DoWork(_cts.Token);
}, cancellationToken);
}

public void StopExecuting(CancellationToken cancellationToken = default)
{
_cts.Cancel();
_task.Wait(cancellationToken);
}

public async Task DoWork(CancellationToken cancellationToken)
{
while (true)
{
try
{
Console.WriteLine("Doing work");
printService.PrintSomething();

await Task.Delay(1000, cancellationToken);
} catch (OperationCanceledException)
{
Console.WriteLine("Operation cancelled");
break;
}
}
}
}
12 replies
CC#
Created by UltraWelfare on 1/25/2024 in #help
Primary constructor parameter is null after being run in a Task
It works as intended, it's just the debugger is showing weird information about the variables
12 replies
CC#
Created by UltraWelfare on 1/25/2024 in #help
Primary constructor parameter is null after being run in a Task
It's used inside DoWork
12 replies
CC#
Created by UltraWelfare on 1/25/2024 in #help
Primary constructor parameter is null after being run in a Task
Also in before you tell me about BackgroundServices: I'm in blazor hybrid where these type of stuff dont work.
12 replies
CC#
Created by UltraWelfare on 1/25/2024 in #help
Primary constructor parameter is null after being run in a Task
Small Update: it isn't null, the debugger is messing something up :/
12 replies
CC#
Created by UltraWelfare on 1/20/2024 in #help
Tips on error handling
The only problem is, if a function doesn't give out a specific error I'd still have to declare a callback for it.. but eh
7 replies
CC#
Created by UltraWelfare on 1/20/2024 in #help
Tips on error handling
It's basically Result | (NotFound | GenericError | List<ValidationFailure>)
7 replies
CC#
Created by UltraWelfare on 1/20/2024 in #help
Tips on error handling
I basically ended up doing something like you said OneOf<Result, Errors> Where Result can be any type but Errors is : OneOf<NotFound, GenericError, List<ValidationFailure>>
7 replies
CC#
Created by UltraWelfare on 1/20/2024 in #help
Tips on error handling
The problem is that sometimes I want a List of validation failures (Fluent validation) other times I want just a string (generic error) and other times I want a Not Found error
7 replies
CC#
Created by UltraWelfare on 1/19/2024 in #help
How to structure query functions in services
Yep, blazor hybrid
22 replies
CC#
Created by UltraWelfare on 1/19/2024 in #help
How to structure query functions in services
I appreciate your input but I'm a bit sceptical of making the consumer of the function be mindful about Includes
22 replies
CC#
Created by UltraWelfare on 1/19/2024 in #help
How to structure query functions in services
Otherwise why bother having a service layer for the query? you could straight up get the context at the controller or in the desktop gui function and do everything there
22 replies