I have some software which manipulates text on the Windows clipboard. Specifically it will remove an underscore _
(and return characters) from the front of clipboard text, then check if the remaining text is a URL. If it is, the URL without the underscore is stored on the clipboard, otherwise the original text is left. To monitor the clipboard for changes, I am using code from the answer to this question, provided by DBKK. It works fine, but randomly stops working after an amount of time which can be anywhere between 5 mins and ~24 hours.
Below is the code which is called on the clipboard event trigger:
private void clipboardMonitor_ClipboardChanged(object sender, ClipboardAssist.ClipboardChangedEventArgs e)
{
lock(lockVar)
{
if (locked)
{
return;
}
else
{
locked = true;
//this.clipboardMonitor.ClipboardChanged -= new System.EventHandler<ClipboardAssist.ClipboardChangedEventArgs>(this.clipboardMonitor_ClipboardChanged);
count++;
label1.Text = count.ToString();
removeUnderscore();
//this.clipboardMonitor.ClipboardChanged += new System.EventHandler<ClipboardAssist.ClipboardChangedEventArgs>(this.clipboardMonitor_ClipboardChanged);
locked = false;
}
}
}
I have added various debugging code. Firstly the counter, which made me realise when I write back to the clipboard it triggers the event again. I added the two lines that unset then set the event, so modifying the clipboard doesn't call the event again while it is running. I also added code which would block the event from running if it hadn't finished the previous run (the if(lock)
part). Eventually the event stops triggering (as can be seen by a counter not being incremented). It is definitely a problem with the event, I added removeUnderscore()
to a button call and that still works fine when the event has stopped triggering.
My theory is that because of the weird way it can call its self, something is going wrong and not adding the event again. I seem unable to lock the the function using a lock object or the boolean value. Any ideas on what might be going wrong?
Edit: I created another program with the same even which only counts clipboard changes. It appears to run fine indefinitely, so the problem is definitely caused by the event calling it's self. I need a robust way to suspend the even while it is executing.