void myTapEventHandler(object sender) {
//starting a new thread doing some tasks (taking pic, sending it over ethernet, setting a value)
//waiting for the new thread to terminate
//upgrading some graphic objects
}
the problem is: if a tap myButton while the spider is processing the time-consuming tasks, a new event is triggered and tasks are performed again
so i tried
i tried, but it didn’t work and i don’t know why
the only explaination i found is that the event is put on something like a queue and it’s handled after myTapEventHandler has returned
heavy processing is already performed on a secondary thread and the main thread waits for its termination (actually, it waits on a AutoResetEvent object)
there is a way to flush the events queue?
Instead of waiting for the secondary thread to terminate, there should be a flag that says the secondary thread is processing. This flag is set by the event handler, when it gets an event and the secondary thread is idle. After the flag is set by the event handler, the handler should wake up the secondary thread. The secondary thread upon wakeup, does it processing, and when done reset the flag. If the event handler receives another event, and the flag is set, it can ignore the event.
Sounds like you are almost there.
In general, with the Micro Framework, it is not good practice to do any waiting or heavy processing in an event handler.
so the solution is setting a flag for choosing how to handle the event (in my case, by doing tasks or by doing nothing)?
i don’t like it because, right now, the secondary thread update only the data model and the main one manage the ui, so it has to wait the data’s update for refreshing the view
there is no way for ignoring events instead of handling them doing nothing?
i resolved the problem implementing a consumer thread and a flag to indicate if it is waiting for a new product, so the ui thread doesn’t wait for a product and can ignore events if an event is already being handled
tnx for the suggestions
From what I see, as long as you fire off a thread in your event handler and then let the event terminate, your code
myButton.OnTapEvent -= OnTapHandler;
should be executed and the event handler removed from the button. The issue is if you stay in your event handler and not return.
I disable a button when I don’t want it to be pressed again until the processing is done. This lets the user know that the system is busy and that they can’t press it again. When the processing is done, the button is re-enabled.
I do like Mike’s idea with the flag though as this is a simple check if you want to keep the event handler active.