Tim
✅ Cancel stream read
I'm running a Task that constantly reads some data off a (named pipe) stream when it's present.
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
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
Readonly struct property vs member variable
What would make the difference between
and
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
❔ Static interface methods
I have several classes implementing my Codec interface like this:
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
✅ 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:
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:
But that is obviously not supported.42 replies
✅ 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
✅ 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