Tim
Tim
CC#
Created by Tim on 12/12/2023 in #help
✅ Cancel stream read
I'm running a Task that constantly reads some data off a (named pipe) stream when it's present.
while (_shouldRun)
{
// .. the below is in another method, but this is the idea
using (var reader = new BinaryReader(_receiveStream, encoding, true))
{
identifier = reader.ReadUInt16();
ushort lenght = reader.ReadUInt16();
data = reader.ReadBytes(lenght);
}
}
while (_shouldRun)
{
// .. the below is in another method, but this is the idea
using (var reader = new BinaryReader(_receiveStream, encoding, true))
{
identifier = reader.ReadUInt16();
ushort lenght = reader.ReadUInt16();
data = reader.ReadBytes(lenght);
}
}
the _shouldRun indicates whether the task should continue, I set this to false when it needs to stop The issue is that the read operation on the stream is infinitely blocking, so I'm never sure if my Task will end properly Can I stop a blocking stream read operation from another thread in some way?
241 replies
CC#
Created by Tim on 12/7/2023 in #help
One time event wait
Heya! I have an event event Action<Message> MessageReceived that gets called whenever a message gets received from another program. I would like to set up some version check, so that the client needs to send a 'version check' message to the server before it will start doing anything. How can I do this cleanly using an event?
19 replies
CC#
Created by Tim on 11/24/2023 in #help
Readonly struct property vs member variable
What would make the difference between
readonly struct Values{
float MyValue { get; }
// .. constructor
}
readonly struct Values{
float MyValue { get; }
// .. constructor
}
and
readonly struct Values{
readonly float MyValue;
// .. constructor
}
readonly struct Values{
readonly float MyValue;
// .. constructor
}
I see the approach using the property lose it's value here, since the struct is already readonly (e.g. copying is useless) Is there a 'better' method between these two? Or better said, why would anyone even use the first approach?
4 replies
CC#
Created by Tim on 10/23/2023 in #help
❔ Static interface methods
I have several classes implementing my Codec interface like this:
public sealed class CodecByte : Codec<byte>
{
public byte Decode(EncodedData data) => data.Buffer[0];

public EncodedData Encode(byte data) => new EncodedData(new byte[] { data });
}
public sealed class CodecByte : Codec<byte>
{
public byte Decode(EncodedData data) => data.Buffer[0];

public EncodedData Encode(byte data) => new EncodedData(new byte[] { data });
}
Is there a better way to represent multiple actions (Encode and Decode) without using a class? - If there would have been a single method, I would start using a delegate. But a delegate can't do multiple things - This is essentially just logic, so having this in a class is a bit useless (and makes me want to turn this into a singleton because instances aren't necessary) So basically looking to eliminate the redundancy of a class (because I only need 1 instance) here while having those 2 functions combined (with a delegate somehow?)
17 replies
CC#
Created by Tim on 10/23/2023 in #help
✅ Enum and generics
Hello there! In our current code, we use structs to represent a packet; Packet<Data>. This packet (struct) has a ushort Identifier and a Codec<Data> Codec. This Identifier should be unique, but currently we assign these manually:
Packet<String> SendMessage = new Packet<String>(0, CodecString.UTF8);
Packet<String> SendMessage = new Packet<String>(0, CodecString.UTF8);
This isn't ideal, so I considered using an Enum to represent the packet Id's, however that would mean that the packets would be in 2 places, the enum and the Packet instance holding the codec. Is there a way to combine this? The ideal solution would be an enum with generic <Data> to allow enforcing the codec used:
public void SendPacket<Data>(PacketEnum<Data> enumType, Data data){}
public void SendPacket<Data>(PacketEnum<Data> enumType, Data data){}
But that is obviously not supported.
42 replies
CC#
Created by Tim on 9/22/2023 in #help
❔ HwndHost not rendering embedded window
No description
19 replies
CC#
Created by Tim on 9/14/2023 in #help
✅ VS Launch Profiles
5 replies
CC#
Created by Tim on 9/13/2023 in #help
✅ Pipe client server
Hey! I've been reading into the C# Pipe stream stuff, because I need some interprocess communication. What I want is 2 processes being able to send a bit of data to each other whenever. All the example's I've seen so far just - Open a stream - Send some data - Recieve some data back - Close the stream Can somebody point me in the right direction on how to do more long lived communication? If I want to support recieving and sending at the same time, do I need multiple Streams per program?
21 replies
CC#
Created by Tim on 9/11/2023 in #help
❔ ✅ Why does this block the UI thread?
public async void FindWindowHandle(HandleWindowFind handleWindowFind)
{
if (!started) throw new InvalidOperationException("Process not started");
if (playerWindowHandle != IntPtr.Zero)
{
handleWindowFind(playerWindowHandle);
return;
}

var finding = IntPtr.Zero;
await Task.Run(() =>
{
while (finding == IntPtr.Zero)
{
Thread.Yield();
finding = GetWindow(_hostWindow, 5);
}
});
handleWindowFind(playerWindowHandle = finding);
}
public async void FindWindowHandle(HandleWindowFind handleWindowFind)
{
if (!started) throw new InvalidOperationException("Process not started");
if (playerWindowHandle != IntPtr.Zero)
{
handleWindowFind(playerWindowHandle);
return;
}

var finding = IntPtr.Zero;
await Task.Run(() =>
{
while (finding == IntPtr.Zero)
{
Thread.Yield();
finding = GetWindow(_hostWindow, 5);
}
});
handleWindowFind(playerWindowHandle = finding);
}
I have this function. Calling this seems to be blocking the thread that it's called from. Did I do something wrong here, or should this not block and maybe something else is causing that?
84 replies
CC#
Created by Tim on 9/7/2023 in #help
✅ Get window handle when it's created
In my WPF app, I want to embed the window from a Process (a Unity player application). I'm extending HwndHost to host it in a control. I create a Process with the executable, and start it. The Unity player obviously takes a bit to load and the window is not created immediately (not even after calling Process.WaitForInputIdle). Is there some way of getting the player window handle immediately when it is created? I tried - Using Process.MainWindowHandle, but that value is not set until it's created, so I'd have to block the thread waiting for it. - Using EnumChildWindows, may be a bit more reliable, but has the same issue as above. Ideally I want to start the player process and when it creates the main window immediately embed it, without it flashing on the screen or showing in the task bar.
15 replies