nathanAjacobs
nathanAjacobs
CC#
Created by nathanAjacobs on 6/5/2024 in #help
NamedPipeClientStream.ReadAsync() continuously reads after the first message arrives
I was being dumb and called dispose in the finally block instead of the catch
5 replies
CC#
Created by nathanAjacobs on 6/5/2024 in #help
NamedPipeClientStream.ReadAsync() continuously reads after the first message arrives
After the first message arrives it continuously reads null as the line
5 replies
CC#
Created by nathanAjacobs on 6/5/2024 in #help
NamedPipeClientStream.ReadAsync() continuously reads after the first message arrives
I don't believe I'm intentionally finishing the server stream, here is my code:
public class ServerPipe : IDisposable
{
readonly SemaphoreSlim semapohre = new SemaphoreSlim(0, 1);

readonly NamedPipeServerStream server;

readonly StreamWriter writer;

private volatile bool _disposed;

public ServerPipe()
{
server = new NamedPipeServerStream("MyTestPipe", PipeDirection.Out);
writer = new StreamWriter(server, System.Text.Encoding.Unicode);
_ = WaitForConnectiionAsync();
}

private async Task WaitForConnectiionAsync()
{
try
{
await server.WaitForConnectionAsync().ConfigureAwait(false);
semapohre.Release();
}
catch (Exception ex)
{
Debug.LogException(ex);
}
}

public async void WriteMessage(string message)
{
if (_disposed)
return;

try
{
await semapohre.WaitAsync().ConfigureAwait(false);
await writer.WriteLineAsync(message).ConfigureAwait(false);
await writer.FlushAsync().ConfigureAwait(false);
}
catch (Exception ex)
{
Debug.LogException(ex);
}
finally
{
semapohre.Release();
Dispose();
}
}

public void Dispose()
{
_disposed = true;
semapohre.Dispose();
writer.Dispose();
server.Dispose();
}
}

public class ClientPipe : IDisposable
{
readonly NamedPipeClientStream client;
readonly StreamReader reader;

public ClientPipe()
{
client = new NamedPipeClientStream(".", "MyTestPipe", PipeDirection.In);
reader = new StreamReader(client);
_ = RunReadLoopAsync();
}

private async Task RunReadLoopAsync()
{
try
{
await client.ConnectAsync();

while (true)
{
string line = await reader.ReadLineAsync();
Debug.Log(line);
}
}
catch (Exception ex)
{
Debug.LogException(ex);
}
}

public void Dispose()
{
reader.Dispose();
client.Dispose();
}
}
public class ServerPipe : IDisposable
{
readonly SemaphoreSlim semapohre = new SemaphoreSlim(0, 1);

readonly NamedPipeServerStream server;

readonly StreamWriter writer;

private volatile bool _disposed;

public ServerPipe()
{
server = new NamedPipeServerStream("MyTestPipe", PipeDirection.Out);
writer = new StreamWriter(server, System.Text.Encoding.Unicode);
_ = WaitForConnectiionAsync();
}

private async Task WaitForConnectiionAsync()
{
try
{
await server.WaitForConnectionAsync().ConfigureAwait(false);
semapohre.Release();
}
catch (Exception ex)
{
Debug.LogException(ex);
}
}

public async void WriteMessage(string message)
{
if (_disposed)
return;

try
{
await semapohre.WaitAsync().ConfigureAwait(false);
await writer.WriteLineAsync(message).ConfigureAwait(false);
await writer.FlushAsync().ConfigureAwait(false);
}
catch (Exception ex)
{
Debug.LogException(ex);
}
finally
{
semapohre.Release();
Dispose();
}
}

public void Dispose()
{
_disposed = true;
semapohre.Dispose();
writer.Dispose();
server.Dispose();
}
}

public class ClientPipe : IDisposable
{
readonly NamedPipeClientStream client;
readonly StreamReader reader;

public ClientPipe()
{
client = new NamedPipeClientStream(".", "MyTestPipe", PipeDirection.In);
reader = new StreamReader(client);
_ = RunReadLoopAsync();
}

private async Task RunReadLoopAsync()
{
try
{
await client.ConnectAsync();

while (true)
{
string line = await reader.ReadLineAsync();
Debug.Log(line);
}
}
catch (Exception ex)
{
Debug.LogException(ex);
}
}

public void Dispose()
{
reader.Dispose();
client.Dispose();
}
}
5 replies
CC#
Created by nathanAjacobs on 5/6/2024 in #help
Can Memory<T> be created from a pointer?
This allocates a managed array and has to perform a copy. The MemoryManager<T> is the better solution
15 replies
CC#
Created by nathanAjacobs on 5/6/2024 in #help
Can Memory<T> be created from a pointer?
Yeah I tried asking copilot earlier and got a lot of stuff that was incorrect
15 replies
CC#
Created by nathanAjacobs on 5/6/2024 in #help
Can Memory<T> be created from a pointer?
Ty this will work! I was going crazy looking for a something in the BCL that does this.
15 replies
CC#
Created by nathanAjacobs on 2/7/2024 in #help
What exactly do these limitations mean for NativeAOT?
Facts
24 replies
CC#
Created by nathanAjacobs on 2/7/2024 in #help
What exactly do these limitations mean for NativeAOT?
Lol of course there already is a source generator, that's awesome
24 replies
CC#
Created by nathanAjacobs on 2/7/2024 in #help
What exactly do these limitations mean for NativeAOT?
This is making more sense
24 replies
CC#
Created by nathanAjacobs on 2/7/2024 in #help
What exactly do these limitations mean for NativeAOT?
Thanks for clarifying, I'll need to read more up on COM. That definitely helps though.
24 replies
CC#
Created by nathanAjacobs on 2/7/2024 in #help
What exactly do these limitations mean for NativeAOT?
The question was, what exactly do the docs mean when it lists Windows: No built-in COM as a limitation for NativeAOT
24 replies
CC#
Created by nathanAjacobs on 2/7/2024 in #help
What exactly do these limitations mean for NativeAOT?
I'll have to do some reading up on COM, I'm curious what the limitation actually is.
24 replies
CC#
Created by nathanAjacobs on 2/7/2024 in #help
What exactly do these limitations mean for NativeAOT?
Mostly makes more sense now ty. I don't really know how COM works, so this might not make sense. If it's not as plug and play as LibraryImport, could there be a source generator similar to LibraryImport, but for COM instead?
24 replies
CC#
Created by nathanAjacobs on 2/7/2024 in #help
What exactly do these limitations mean for NativeAOT?
So basically anything using COM directly in C# won't work, but P/Invoke should still be fine right if using LibraryImport?
24 replies
CC#
Created by Ploxi on 2/7/2024 in #help
Threadsafety and Interlocked.CompareExchange
Why not just use a ConcurrentDictionary?
53 replies
CC#
Created by nathanAjacobs on 1/19/2024 in #help
Optional parameters with dependency injection
So if it exists in a library, I don't know if the consumer actually has an ILogger registered
10 replies
CC#
Created by nathanAjacobs on 1/19/2024 in #help
Optional parameters with dependency injection
Those are all fair, but what if this code is in a library, and it might not necessarily be used by dependency injection. So having it as an optional parameter might suit the situation where it is just being newed up manually
10 replies
CC#
Created by nathanAjacobs on 12/27/2023 in #help
Is there any difference between named arguments and default constructor arguments for an attribute?
Thanks for that insight!
3 replies
CC#
Created by stigzler on 10/29/2023 in #help
❔ How to stop WebClient.DownloadString encoding apostrophies?
8 replies
CC#
Created by stigzler on 10/29/2023 in #help
❔ How to stop WebClient.DownloadString encoding apostrophies?
8 replies