C
Join ServerC#
help
❔ Events vs Tasks
DD.Mentia1/30/2023
In what situation would you use an Event over a Task that might wrap the event? As an example:
vs
The second implementation is (obviously, to me) cleaner and easier to understand, all the logic together and with its vars staying in scope, for the situations where a process relies on multiple events.
Are there any memory or performance concerns with doing it that way? Or other good reasons to not do that
(Assuming I am just wrapping a basic Event with a TaskCompletionSource that's handled somewhere else)
(And assuming I'm using async Events correctly, which I haven't really looked into how to do them without async voiding but I assume it's possible...)
private Component dragItem;
public void OnMousePressed(EventArgs e)
{
dragItem = getItem(e.Position);
}
public void OnMouseReleased(EventArgs e)
{
dragItem.Position = e.Position;
}
vs
public async void OnMousePressed(EventArgs e)
{
var dragItem = getItem(e.Position);
var released = await MouseReleasedEvent();
dragItem.Position = released.Position;
}
The second implementation is (obviously, to me) cleaner and easier to understand, all the logic together and with its vars staying in scope, for the situations where a process relies on multiple events.
Are there any memory or performance concerns with doing it that way? Or other good reasons to not do that
(Assuming I am just wrapping a basic Event with a TaskCompletionSource that's handled somewhere else)
(And assuming I'm using async Events correctly, which I haven't really looked into how to do them without async voiding but I assume it's possible...)
AAntonC1/30/2023
tasks are essentially one time event handlers
AAntonC1/30/2023
good for stateful workflows
AAntonC1/30/2023
for housekeeping, like e.g. counters in a game, events work better
AAntonC1/30/2023
wrapping an event in a task is a good idea
AAntonC1/30/2023
the other direction feels weird to me
AAntonC1/30/2023
performance? I don't think so really
AAntonC1/30/2023
a task completion source is just a tad bit more housekeeping
DD.Mentia1/30/2023
The question is mostly about defining those two different situations, when events are appropriate over tasks. Because... why not make both, use whichever one makes the most sense
AAntonC1/30/2023
yeah, make both, sure
AAntonC1/30/2023
as long as it helps simplify your logic, make both
AAntonC1/30/2023
.
AAccord1/31/2023
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.