User with super speed?

While supporting a self-service kiosk at a supermarket, I encountered a strange issue: the kiosk would print multiple receipts for a single transaction. At first glance, it seemed like the user had superhuman speed — the logs showed the button event firing multiple times within seconds. Was someone really spamming the button that quickly?

2024-04-27 14:54:21.747 [DEBUG] - btnDone_Click
2024-04-27 14:54:21.778 [DEBUG] - btnDone_Click
2024-04-27 14:54:21.778 [DEBUG] - btnDone_Click
2024-04-27 14:54:21.778 [DEBUG] - btnDone_Click
2024-04-27 14:54:21.778 [DEBUG] - btnDone_Click
2024-04-27 14:54:21.778 [DEBUG] - btnDone_Click
2024-04-27 14:54:21.778 [DEBUG] - btnDone_Click
2024-04-27 14:54:21.778 [DEBUG] - btnDone_Click
2024-04-27 14:54:21.778 [DEBUG] - btnDone_Click
2024-04-27 14:54:21.778 [DEBUG] - btnDone_Click
2024-04-27 14:54:21.778 [DEBUG] - btnDone_Click
2024-04-27 14:54:21.778 [DEBUG] - btnDone_Click
2024-04-27 14:54:21.778 [DEBUG] - btnDone_Click
2024-04-27 14:54:21.778 [DEBUG] - btnDone_Click
2024-04-27 14:54:21.778 [DEBUG] - btnDone_Click
2024-04-27 14:54:21.778 [DEBUG] - btnDone_Click
2024-04-27 14:54:21.778 [DEBUG] - btnDone_Click
2024-04-27 14:54:21.778 [DEBUG] - btnDone_Click
2024-04-27 14:54:21.778 [DEBUG] - btnDone_Click
2024-04-27 14:54:21.778 [DEBUG] - btnDone_Click

After some investigation, I discovered the root cause: the button click event handler was being attached multiple times without being properly unsubscribed. Each time the user navigated between certain pages, a new handler was added — leading to multiple invocations when the button was clicked.

public void Initializer()
{
  // Addition assignment operator (+=) to attach an event handler to the event.
  this.DoneClick += btnDone_Click;
}

public void Deinitializer()
{
  this.DoneClick -= btnDone_Click; // ⚠️ Detaching the event handler to avoid multiple subscriptions.
}

Lesson learned: super speed is still fictional (for now), but forgetting to unsubscribe event handlers is a very real bug. Always manage your event subscriptions properly to avoid side effects like this.

Further reading: How to subscribe to and unsubscribe from events - C# | Microsoft Learn