0

I have a Wacom Epad. Initially I subscribe to onPenData event on my class constructor and I unsubscribe from it on device disconnection.

When user does multiples clicks on the Wacom Epad screen, it fires the onPenData event to which I am subscribed.

private void onPenData(wgssSTU.IPenData penData) // Process incoming pen data
{
     // Do some stuff
} 

So what I am trying to do is to implement some kind of mechanism to avoid onPenData to be executed on repeated clicks.

How can I do this?

Willy
  • 9,848
  • 22
  • 141
  • 284
  • You can try setting a global variable is clicked and checking it before executing the method logic. Im not sure that is what you meant. If you meant don't fire between a time period lets say 5 seconds than you could use a timer. – Merv Jul 18 '18 at 11:53
  • Have no idea what "Wacom Epad" is, but what's wrong with firing event multiple times? Please, explain problem details. – Dennis Jul 18 '18 at 11:59
  • @user1624552, if your code allow to set flag then please add flag=false once your event subscribed and then for successive multiple clicks if flag=false then event will not subscribed again bcoz flag value is not true. i think may this help you – er-sho Jul 18 '18 at 12:07
  • @Dennis It is a kind of tablet that user clicks on it using a pen. So when user clicks on it using the pen repeated times by leaving between clicks less than 1 second for example, I mean, user clicks very quicly on it a lot of repeated times, my program crashes saying insuficient memory. – Willy Jul 18 '18 at 12:22

1 Answers1

2

You can't prevent event handler firing, but you can manage event handling logic execution.

Suppose, that there are event handler onPenData and OnPenDataBusinessLogic method:

private void onPenData(wgssSTU.IPenData penData) // Process incoming pen data
{
    OnPenDataBusinessLogic(penData);
} 

private void OnPenDataBusinessLogic(wgssSTU.IPenData penData)
{
     // Do some stuff
}

Now you have number of options:

1) bool flag. The easiest one:

private bool onPenDataRunning;

private void onPenData(wgssSTU.IPenData penData) // Process incoming pen data    
{
    if (onPenDataRunning)
    {
        return;
    }

    onPenDataRunning = true;
    try
    {
        OnPenDataBusinessLogic(penData);
    }
    finally
    {
        onPenDataRunning = false;
    }
} 

2) OnPenDataBusinessLogic delayed execution. Every time onPenData fired, (re-)start a timer. When timer is elapsed, call OnPenDataBusinessLogic.

3) Pen data buffering. Buffer data in onPenData, process pen data in background thread/task via OnPenDataBusinessLogic.

Dennis
  • 37,026
  • 10
  • 82
  • 150
  • Another option would be to ignore next times event is fired if some seconds/milliseconds have not been elapsed as explained here: https://stackoverflow.com/questions/841332/prevent-double-click-from-double-firing-a-command/841668#841668 – Willy Jul 19 '18 at 18:23
  • The option 1 you posted is not thread-safe. onPenDataRunning should be protected using a lock. – Willy Jul 19 '18 at 18:35