By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,667 Members | 1,321 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,667 IT Pros & Developers. It's quick & easy.

Preventing Key/Mouse Events from Interruping Processing

P: n/a
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
Nov 13 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
On Mon, 7 Jul 2003 22:43:02 -0400, "Bill Henning" <please @
nospamforactiprosoftware.com> wrote:
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.
--
Abderaware
Fine Components For .NET
Turn on, tune in, download.
zane a@t abderaware.com
Nov 13 '05 #2

P: n/a
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

"Bill Henning" <please @ nospamforactiprosoftware.com> schreef in bericht
news:#i**************@TK2MSFTNGP10.phx.gbl...
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

Nov 13 '05 #3

P: n/a
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

"phoenix" <pa******@skynetWORK.be> wrote in message
news:O9**************@TK2MSFTNGP10.phx.gbl...
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

"Bill Henning" <please @ nospamforactiprosoftware.com> schreef in bericht
news:#i**************@TK2MSFTNGP10.phx.gbl...
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


Nov 13 '05 #4

P: n/a
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

Bill Henning wrote:
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


Nov 13 '05 #5

P: n/a
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" <mg*******@stop.spam.taxussi.com.pl> wrote in message
news:be**********@nemesis.news.tpi.pl...
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

Bill Henning wrote:
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

Nov 13 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.