There is a way to ignore touch events?

hi
let’s start with the code:

GHI.Glide.Display.Window window = GlideLoader.LoadWindow(xml string);
GHI.Glide.UI.Button myButton = (GHI.Glide.UI.Button)window.GetChildByName("myButton");
OnTap OnTapHandler = new OnTap(myTapEventHandler);
myButton.OnTapEvent += OnTapHandler;

void myTapEventHandler(object sender) {
   //doing time-consuming tasks
}

myTapEventHandler looks like:

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

void myTapEventHandler(object sender) {
   myButton.OnTapEvent -= OnTapHandler;
   //doing time-consuming tasks
   myButton.OnTapEvent += OnTapHandler;
}

and

void myTapEventHandler(object sender) {
   GlideTouch.IgnoreAllEvents = true;
   //doing time-consuming tasks
   GlideTouch.IgnoreAllEvents = false;
}

but they didn’t work

what should i do?

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

you are correct about the event queuing.

try using a separate thread for the heavy processing. if you get a second event while the processing is happening, you can ignore it.

this a modified producer/ consumer pattern.

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?

No, there is no way to flush the event 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?

@ fez27 - no way except in your logic

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 :wink:

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.

@ Dave McLaughlin - you could have a race condition. another event could happen before you removed the event handler.

the time delay between event and handler dispatch makes life difficult.

True, which is why I prefer the flag solution you mentioned or disable the button as the first call to the event handler.