Preventing Key/Mouse Events from Interruping Processing

  • Thread starter Thread starter Bill Henning
  • Start date Start date
B

Bill Henning

Does anyone know a good method of preventing keyboard and mouse events from
interrupting processing?

My situation is:
1) I need to track and handle all key and mouse events
2) I need to perform processing on certain key/mouse events
3) If key/mouse events interrupt processing, the events should not be
discarded since they need to be handled but AFTER the current processing is
complete

Right now, if I press 3 keys really fast and track console messages (to know
what sequence of code executes) I get something like this:
* OnKeyPress(a)
* ProcessingStart(a)
* OnKeyPress(b)
* ProcessingStart(b)
* OnKeyPress(c)
* ProcessingStart(c)
* ProcessingEnd(c)
* ProcessingEnd(b)
* ProcessingEnd(a)

Where this is what I need but don't know how to make happen:
* OnKeyPress(a)
* ProcessingStart(a)
* ProcessingEnd(a)
* OnKeyPress(b)
* ProcessingStart(b)
* ProcessingEnd(b)
* OnKeyPress(c)
* ProcessingStart(c)
* ProcessingEnd(c)

Any help is MUCH appreciated!

Bill
 
Does anyone know a good method of preventing keyboard and mouse events from
interrupting processing?

No, but perhaps you should just put the relevant information in a queue and pull
it out with a second thread or upon the occurance of a timer interval.
 
I think you should solve this with a queue and a boolean.

-- pseudocode --

Queue q
bool isBusy = false;

OnKeyPress(key) {
q.add(key);
// In case nothing is going on
if (!isBusy) ProcessNext();
}

ProcessNext() {
isBusy = true;
while (!q.isEmpty()) {
// Process the top one
}
// Nothing more in the queue, make sure
// that on the following keypress ProcessNext()
// will be called again
isBusy = false;
}

-- end pseudocode --

HTH

Yves
 
Thanks for replying Zane and Yves. The queue idea is probably what I'm
going to have to do.

It would be nice if there was some way in .NET to make that processing into
a critical section to prevent this from happening however since it's all the
same thread, that's not possible I guess.

Thanks again,
Bill
 
Hi Bill,

Maybe i don't understand your problem?
If i run form in [STAThread] mode, my events are fired in
queue as they were started.

Try this code (after placing ListBox control on your form):

private void LongTimeNothing() {
int i,j,k;
int maxVal=400;
for(i=0; i<maxVal; i++) {
for(j=0; j<maxVal; j++) {
for(k=0; k<maxVal; k++) {
int val=i+j+k;
}
}
}
}

private void listBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs ea) {
listBox1.Items.Add(String.Format("KeyPress for \'{0}\'", ea.KeyChar));
this.LongTimeNothing();
listBox1.Items.Add(String.Format("KeyPress for \'{0}\'", ea.KeyChar));
}

private void listBox1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs ea) {
listBox1.Items.Add(String.Format("MouseDown for \'{0}\'", ea.Button));
this.LongTimeNothing();
listBox1.Items.Add(String.Format("MouseDown for \'{0}\'", ea.Button));
}

....then set listBox1 events for "KeyPress" & "MouseDown" to
proper methods in code

Regards

Marcin Grzêbski
 
Marcin,

I think you're right... I looked over my huge amount of processing code and
found an Application.DoEvents(). When I took that out, it started working
again sequentially as it should. I've learned my lesson with not to use the
DoEvents! :)

Thanks,
Bill


Marcin Grzêbski said:
Hi Bill,

Maybe i don't understand your problem?
If i run form in [STAThread] mode, my events are fired in
queue as they were started.

Try this code (after placing ListBox control on your form):

private void LongTimeNothing() {
int i,j,k;
int maxVal=400;
for(i=0; i<maxVal; i++) {
for(j=0; j<maxVal; j++) {
for(k=0; k<maxVal; k++) {
int val=i+j+k;
}
}
}
}

private void listBox1_KeyPress(object sender,
System.Windows.Forms.KeyPressEventArgs ea) {
listBox1.Items.Add(String.Format("KeyPress for \'{0}\'", ea.KeyChar));
this.LongTimeNothing();
listBox1.Items.Add(String.Format("KeyPress for \'{0}\'", ea.KeyChar));
}

private void listBox1_MouseDown(object sender,
System.Windows.Forms.MouseEventArgs ea) {
 
Back
Top