Understanding Application.DoEvents

B

bazad

Hi,

I'd like to understand consequences of Application.DoEvents call.
Does it create a new thread?


Thank you
 
W

Willy Denoyette [MVP]

bazad said:
Hi,

I'd like to understand consequences of Application.DoEvents call.
Does it create a new thread?


Thank you

No, it doesn't create a new thread, it 'possibly' runs a modal message loop
(peekmessage/getmessage/dispatchmessage stuff) on the current thread.

'possibly' here means- under certain conditions- like a window handle
associated with the thread, and running an interactive session.

Willy.
 
B

bazad

Willy Denoyette said:
No, it doesn't create a new thread, it 'possibly' runs a modal message loop
(peekmessage/getmessage/dispatchmessage stuff) on the current thread.

'possibly' here means- under certain conditions- like a window handle
associated with the thread, and running an interactive session.

Willy.

Does this mean that user can click on menu items while main UI thread
is busy?
 
W

Willy Denoyette [MVP]

Does this mean that user can click on menu items while main UI thread
is busy?

I guess you are hiding something behind this question :).
The main UI thread runs his own message loop, so calling DoEvents is not
needed to have a responsive UI. If the UI is not responsive, it's an
indication for a blocked waiting thread, so including DoEvents in your code
won't help as it won't execute.

Willy.
 
B

bazad

Willy Denoyette said:
I guess you are hiding something behind this question :).
The main UI thread runs his own message loop, so calling DoEvents is not
needed to have a responsive UI. If the UI is not responsive, it's an
indication for a blocked waiting thread, so including DoEvents in your code
won't help as it won't execute.

Willy.

I guess I do :)

I have a button click event handler which takes a while to execute.
During the execution of the event handler the UI does not repaint
itself. I thought that I could use DoEvents inside some loop which
takes time to execute.
 
W

Willy Denoyette [MVP]

Tell me precisely where I'm wrong.
If your UI thread is blocked waiting, it's blocked, how can a blocked
thread handle windows messages?

Willy.
 
W

Willy Denoyette [MVP]

bazad said:
I guess I do :)

I have a button click event handler which takes a while to execute.
During the execution of the event handler the UI does not repaint
itself. I thought that I could use DoEvents inside some loop which
takes time to execute.

Maybe I wasn't clear when answering your question.
As long as the UI thread runs (a loop for instance) in your handler you can
insert DoEvents calls in order to pump/dispatch messages, but if you make a
single blocking call in your handler (for instance calling a remote
procedure, a network request that takes time to return etc...) inserting
DoEvents makes no sense as the thread is blocked waiting for the call-out to
return.
But again what's your problem exactly, I suppose you did try the DoEvents
call but without success, or am I wrong?

Willy.


Willy.
 
J

Jon Skeet [C# MVP]

Willy Denoyette said:
I guess you are hiding something behind this question :).
The main UI thread runs his own message loop, so calling DoEvents is not
needed to have a responsive UI. If the UI is not responsive, it's an
indication for a blocked waiting thread, so including DoEvents in your code
won't help as it won't execute.

That's not necessarily true. If the UI is not responsive, it may be
because it's doing a lot of work, but isn't blocked. In that case,
calling DoEvents periodically will make the UI more responsive. It also
introduces the possibility of re-entrancy problems, and is generally a
nasty hack IMO.
 
S

ShaneB

I disagree with Willy's answer. Application.DoEvents forces your
application to break from where it is and process all messages within the
application's message queue...messages like WM_PAINT for one. messages in
your case. After that, it returns. This will fix your problem.

ShaneB
 
S

ShaneB

It simply forces the application to process the messages in it's message
queue before it returns. Typically, these are messages like WM_PAINT,
WM_TIMER, etc... It does not create a new thread.

ShaneB
 
S

ShaneB

He said he has a button handler that takes a while to execute. He did not
say anything about blocking the thread from executing...(such as a
Socket.Send call for example). I imagine what he's doing is some loop
that's taking a while and his form is not painting. Application.DoEvents
somewhere in that function will fix that.

After reading your other post a little further down, I think we're saying
the same thing.

ShaneB
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top