maxmahem
maxmahem
CC#
Created by maxmahem on 5/7/2024 in #help
Async events firing out of expected sequence.
Obviously I've got some assumptions wrong here but my thinking was that in these sets of methods would still fire in sequence:
public async Task StopService() {
if (this.service is null) throw new NullReferenceException();

await this.service.StopAsync();

this.service.Dispose();
this.service = null;

this.logStream?.Flush();
this.logStream?.Dispose();
this.logFileStream?.Dispose();

OnPropertyChanged(nameof(ServiceRunning));
OnPropertyChanged(nameof(CanServiceStart));
}
public async Task StopService() {
if (this.service is null) throw new NullReferenceException();

await this.service.StopAsync();

this.service.Dispose();
this.service = null;

this.logStream?.Flush();
this.logStream?.Dispose();
this.logFileStream?.Dispose();

OnPropertyChanged(nameof(ServiceRunning));
OnPropertyChanged(nameof(CanServiceStart));
}
this.service.StopAsync() calls...
public async Task StopAsync()
{
if (this.cancellationTokenSource is not null) await this.cancellationTokenSource.CancelAsync();
this.listener.Stop();
OnMessage(this.listener.LocalEndpoint, $"{Name} Service Stopped.");
}
public async Task StopAsync()
{
if (this.cancellationTokenSource is not null) await this.cancellationTokenSource.CancelAsync();
this.listener.Stop();
OnMessage(this.listener.LocalEndpoint, $"{Name} Service Stopped.");
}
And my thinking was that StopService would halt on that await until StopAsync returned, but it must not be so because the OnMessage is firing after the dispose methods. So... something isn't right. I can probably turn StopAsync synchronous but I'd rather figure out WTF is going on here.
37 replies
CC#
Created by maxmahem on 5/6/2024 in #help
Call from Invalid Thread possibly related to Avalonia
Getting a "Call from Invalid Thread" exception when running this chunk of code from an Avalonia context.
public async Task StartAsync(CancellationToken? token = null) {
this.tokenSource = CancellationTokenSource.CreateLinkedTokenSource(token ?? new CancellationToken());
var internalToken = tokenSource.Token;
this.listener.Start();
OnMessage(this.listener.LocalEndpoint, $"{Name} Service Started...");

try {
while (!internalToken.IsCancellationRequested) {
await Task.Run(async () => {
var tcpClient = await this.listener.AcceptTcpClientAsync(internalToken);
OnMessage(tcpClient.Client.RemoteEndPoint, "Client connected.");
await HandleClientAsync(tcpClient, internalToken);
}, internalToken);
}
}
catch (Exception exception) {
OnMessage(this.listener.LocalEndpoint, $"Exception: {exception.Message}");
}
finally {
this.listener.Stop();
}
}
public async Task StartAsync(CancellationToken? token = null) {
this.tokenSource = CancellationTokenSource.CreateLinkedTokenSource(token ?? new CancellationToken());
var internalToken = tokenSource.Token;
this.listener.Start();
OnMessage(this.listener.LocalEndpoint, $"{Name} Service Started...");

try {
while (!internalToken.IsCancellationRequested) {
await Task.Run(async () => {
var tcpClient = await this.listener.AcceptTcpClientAsync(internalToken);
OnMessage(tcpClient.Client.RemoteEndPoint, "Client connected.");
await HandleClientAsync(tcpClient, internalToken);
}, internalToken);
}
}
catch (Exception exception) {
OnMessage(this.listener.LocalEndpoint, $"Exception: {exception.Message}");
}
finally {
this.listener.Stop();
}
}
(excecption happens at var tcpClient = await this.listener.AcceptTcpClientAsync(internalToken);). Doesn't happen when called from a CLI context.
8 replies
CC#
Created by maxmahem on 5/5/2024 in #help
Canceling TcpClient after TcpListener has been stopped
So, I may have done this completely right, but I'm a bit unsure on the behavior. I'm creating TcpClients with AcceptTcpClientAsync and passing it a CancellationToken which does seem to end the TcpClients, but I guess I'm expecting to see a more graceful end in my TcpCleints which I am not. To wit, I'm expecting to end up hitting client.Close and then get the "Client disconnected" message, which isn't happening.
48 replies