Rome
Rome
CC#
Created by Rome on 2/4/2024 in #help
Handling events in "blocks"
@Pobiega So, I figured out that the problem was that we kept starting a new loop everytime we received a button pressed event. We should have only started the loop on the first event and just add to the requests on subsequent event. Here is the updated event handler:
private void OnButtonPushed(object sender, ElevatorControllerEventArgs e)
{
eventsReceived++;
// start elevator on first event received
if (eventsReceived == 1)
{
requestsQueue.AddLast(e.Floor);
MoveElevator();
}
// else just add floor to request to avoid multiple loops running
else
{
requestsQueue.AddLast(e.Floor);
}
}
private void OnButtonPushed(object sender, ElevatorControllerEventArgs e)
{
eventsReceived++;
// start elevator on first event received
if (eventsReceived == 1)
{
requestsQueue.AddLast(e.Floor);
MoveElevator();
}
// else just add floor to request to avoid multiple loops running
else
{
requestsQueue.AddLast(e.Floor);
}
}
96 replies
CC#
Created by Rome on 2/4/2024 in #help
Handling events in "blocks"
Thank you for your help and good night 🙂 ... I'll do some more work on this and will get back to you once I have the solution
96 replies
CC#
Created by Rome on 2/4/2024 in #help
Handling events in "blocks"
No, I mean the MoveElevator() method in the Logic class.
96 replies
CC#
Created by Rome on 2/4/2024 in #help
Handling events in "blocks"
Did you change how it processes the queue? I'm not fully understanding it but I like the idea of checking if the elevator is running. I'll explore that some more and see what I can come up with.
96 replies
CC#
Created by Rome on 2/4/2024 in #help
Handling events in "blocks"
I think you might have to post your code since I don't understand it at all lol. I'll have to read your code to really understand your approach. Do you think an approach using EventHandlerList is viable? I was reading this documentation and thought it might be promising https://learn.microsoft.com/en-us/dotnet/standard/events/how-to-handle-multiple-events-using-event-properties
96 replies
CC#
Created by Rome on 2/4/2024 in #help
Handling events in "blocks"
Not familiar at all
96 replies
CC#
Created by Rome on 2/4/2024 in #help
Handling events in "blocks"
I believe that's the correct output for i9,o8,o2,d3,i7,i4,o3. Yeah, you're correct about the elevator being at floor 2 when the events for 8, 2 happen. It should still be on floor 1 but I also see no way around. The only workaround I see is to pass the input sequence to the Logic class so it knows how many "groups" of inputs are coming in but unfortunately that's against the requirements. I have an idea of keeping track of the units of time that's passed for each move (Up, Down, Stop) and somehow use that to keep track of each grouping but I haven't quite figured out how I'll do that lol. How are you getting it to stop at floor 2 for 8, 2? Did you switch the structure of the loop or add if statements in the while loop?
96 replies
CC#
Created by Rome on 2/4/2024 in #help
Handling events in "blocks"
I have to leave for a few hours. I'll try to think about the problem while I'm gone but if you do find the solution please don't give it to me right away. Maybe just give a hint to point me in the proper direction 🙂 ... thanks for your help!
96 replies
CC#
Created by Rome on 2/4/2024 in #help
Handling events in "blocks"
Oh that's perfect. I was getting annoyed by the character limit lol. Here's what I have so far. The only part that's stopping me from meeting the requirements is processing each "block" of inputs together. The logic file is the only thing that can be edited. https://paste.mod.gg/lljkatnacblm/0
96 replies
CC#
Created by Rome on 2/4/2024 in #help
Handling events in "blocks"
just a console app
96 replies
CC#
Created by Rome on 2/4/2024 in #help
Handling events in "blocks"
each MoveUp, MoveDown, and Stop is 1 unit of time
96 replies
CC#
Created by Rome on 2/4/2024 in #help
Handling events in "blocks"
that's exactly how it's supposed to go
96 replies
CC#
Created by Rome on 2/4/2024 in #help
Handling events in "blocks"
Pretty much, yes. The controller returns the destination floor when the event happens (e.g. 9 8 2). The methods to Stop, MoveUp, and MoveDown are also exposed but that doesn't affect the event order in any way.
96 replies
CC#
Created by Rome on 2/4/2024 in #help
Handling events in "blocks"
Sure, I did not paste the in between steps where the queue is updated as I did not think the logic for that was important to the problem but I updated the original post.
96 replies
CC#
Created by Rome on 2/4/2024 in #help
Handling events in "blocks"
yes and no. It starts moving to 9 but somewhere between 1 and 9 the event for 8 occurs and the queue is updated accordingly. It will stop at 8. The problem is that it should've stopped at 2 as well.
96 replies
CC#
Created by Rome on 2/4/2024 in #help
Handling events in "blocks"
Trust me, me neither LOL. My thought process was to delay before calling MoveElevator but I had no idea what I was doing so I scrapped the idea but it could work?
96 replies
CC#
Created by Rome on 2/4/2024 in #help
Handling events in "blocks"
I was googling around and was thinking a delay could work or a ManualResetEvent but I was unsure as where to place it. It could be a valid solution as I have not ruled it out.
96 replies
CC#
Created by Rome on 2/4/2024 in #help
Handling events in "blocks"
correct, yes
96 replies
CC#
Created by Rome on 2/4/2024 in #help
Handling events in "blocks"
At index 0 it'll process 9,8,2 index 1 - nothing index 2 - nothing index 3 - it'll process 7,4,3
96 replies
CC#
Created by Rome on 2/4/2024 in #help
Handling events in "blocks"
The controller processes the events in order of the sequence order. Here is the part that's relevant to that process. My example sequence above would look something like i9,o8,o2,d3,i7,i4,o3. I removed the i's and o's because they're not relevant to the order, however the d's are.
private void ProcessEvent(string ev)
{
if(ev.StartsWith("O"))
{
int floor = 0;
if(Int32.TryParse(ev.Substring(1), out floor) && null != OutsideButtonPushed)
{
OutsideButtonPushed(this, new ElevatorControllerEventArgs(floor));
}
}
else if(ev.StartsWith("I"))
{
int floor = 0;
if(Int32.TryParse(ev.Substring(1), out floor) && null != InsideButtonPushed)
{
InsideButtonPushed(this, new ElevatorControllerEventArgs(floor));
}
}
}
private void ProcessEvent(string ev)
{
if(ev.StartsWith("O"))
{
int floor = 0;
if(Int32.TryParse(ev.Substring(1), out floor) && null != OutsideButtonPushed)
{
OutsideButtonPushed(this, new ElevatorControllerEventArgs(floor));
}
}
else if(ev.StartsWith("I"))
{
int floor = 0;
if(Int32.TryParse(ev.Substring(1), out floor) && null != InsideButtonPushed)
{
InsideButtonPushed(this, new ElevatorControllerEventArgs(floor));
}
}
}
ProcessEvent() is called in proper order of the sequence i9,o8,o2,d3,i7,i4,o3. The sequence is stored in a dictionary that'll look like { {0: [i9,o8,o2], 3: [i7,i4,o3] } } . A for loop goes through the sequence, here's the part that does that.
for(int pastIndex = prevStart; pastIndex < sequenceIndex; pastIndex++)
{
if(sequencedEvents.ContainsKey(pastIndex))
{
List<string> eventsAtIndex = sequencedEvents[pastIndex];
if(eventsAtIndex.Count > 0)
{
prevStart = pastIndex + 1;
foreach(string ev in eventsAtIndex)
{
ProcessEvent(ev);
}
}
eventsAtIndex.Clear();
}
}
for(int pastIndex = prevStart; pastIndex < sequenceIndex; pastIndex++)
{
if(sequencedEvents.ContainsKey(pastIndex))
{
List<string> eventsAtIndex = sequencedEvents[pastIndex];
if(eventsAtIndex.Count > 0)
{
prevStart = pastIndex + 1;
foreach(string ev in eventsAtIndex)
{
ProcessEvent(ev);
}
}
eventsAtIndex.Clear();
}
}
96 replies