473,411 Members | 1,923 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,411 software developers and data experts.

Understanding Application.DoEvents

Hi,

I'd like to understand consequences of Application.DoEvents call.
Does it create a new thread?
Thank you
Jul 21 '05 #1
24 3198

"bazad" <no****@noreply.not> wrote in message
news:1099593932.4+jIsAHupOrTgThYXFT72g@teranews...
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.
Jul 21 '05 #2
"Willy Denoyette [MVP]" <wi*************@pandora.be> writes:
"bazad" <no****@noreply.not> wrote in message
news:1099593932.4+jIsAHupOrTgThYXFT72g@teranews...
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.


Does this mean that user can click on menu items while main UI thread
is busy?
Jul 21 '05 #3

"bazad" <no****@noreply.not> wrote in message
news:1099605909.kJ+tXbnQcb3AE70X6mbdMw@teranews...
"Willy Denoyette [MVP]" <wi*************@pandora.be> writes:
"bazad" <no****@noreply.not> wrote in message
news:1099593932.4+jIsAHupOrTgThYXFT72g@teranews...
> Hi,
>
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.
Jul 21 '05 #4
"Willy Denoyette [MVP]" <wi*************@pandora.be> writes:
"bazad" <no****@noreply.not> wrote in message
news:1099605909.kJ+tXbnQcb3AE70X6mbdMw@teranews...
"Willy Denoyette [MVP]" <wi*************@pandora.be> writes:
"bazad" <no****@noreply.not> wrote in message
news:1099593932.4+jIsAHupOrTgThYXFT72g@teranews...
> Hi,
>

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.


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.
Jul 21 '05 #5
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

"bazad" <no****@noreply.not> wrote in message
news:1099610547.P7cij9OT9nkECcP+fXHq2w@teranews...
"Willy Denoyette [MVP]" <wi*************@pandora.be> writes:
"bazad" <no****@noreply.not> wrote in message
news:1099605909.kJ+tXbnQcb3AE70X6mbdMw@teranews...
> "Willy Denoyette [MVP]" <wi*************@pandora.be> writes:
>
>> "bazad" <no****@noreply.not> wrote in message
>> news:1099593932.4+jIsAHupOrTgThYXFT72g@teranews...
>> > Hi,
>> >

> 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.


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.

Jul 21 '05 #6
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
"bazad" <no****@noreply.not> wrote in message
news:1099593932.4+jIsAHupOrTgThYXFT72g@teranews...
Hi,

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

Jul 21 '05 #7
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.

"ShaneB" <st********@yahoo.com> wrote in message
news:e1*************@TK2MSFTNGP11.phx.gbl...
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

"bazad" <no****@noreply.not> wrote in message
news:1099610547.P7cij9OT9nkECcP+fXHq2w@teranews...
"Willy Denoyette [MVP]" <wi*************@pandora.be> writes:
"bazad" <no****@noreply.not> wrote in message
news:1099605909.kJ+tXbnQcb3AE70X6mbdMw@teranews...
> "Willy Denoyette [MVP]" <wi*************@pandora.be> writes:
>
>> "bazad" <no****@noreply.not> wrote in message
>> news:1099593932.4+jIsAHupOrTgThYXFT72g@teranews...
>> > Hi,
>> >

> 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.


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.


Jul 21 '05 #8

"bazad" <no****@noreply.not> wrote in message
news:1099610547.P7cij9OT9nkECcP+fXHq2w@teranews...
"Willy Denoyette [MVP]" <wi*************@pandora.be> writes:
"bazad" <no****@noreply.not> wrote in message
news:1099605909.kJ+tXbnQcb3AE70X6mbdMw@teranews...
> "Willy Denoyette [MVP]" <wi*************@pandora.be> writes:
>
>> "bazad" <no****@noreply.not> wrote in message
>> news:1099593932.4+jIsAHupOrTgThYXFT72g@teranews...
>> > Hi,
>> >

> 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.


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.
Jul 21 '05 #9
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

"Willy Denoyette [MVP]" <wi*************@pandora.be> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
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.

"ShaneB" <st********@yahoo.com> wrote in message
news:e1*************@TK2MSFTNGP11.phx.gbl...
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

"bazad" <no****@noreply.not> wrote in message
news:1099610547.P7cij9OT9nkECcP+fXHq2w@teranews...
"Willy Denoyette [MVP]" <wi*************@pandora.be> writes:

"bazad" <no****@noreply.not> wrote in message
news:1099605909.kJ+tXbnQcb3AE70X6mbdMw@teranews...
> "Willy Denoyette [MVP]" <wi*************@pandora.be> writes:
>
>> "bazad" <no****@noreply.not> wrote in message
>> news:1099593932.4+jIsAHupOrTgThYXFT72g@teranews...
>> > Hi,
>> >

> 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.

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.



Jul 21 '05 #10
Willy Denoyette [MVP] <wi*************@pandora.be> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #11
bazad <no****@noreply.not> wrote:
I have a button click event handler which takes a while to execute.


In that case, it should execute on a different thread.
Application.DoEvents is a sort of band-aid solution to the problem, but
threading is a much better solution, even though it's more complicated.

See http://www.pobox.com/~skeet/csharp/t...winforms.shtml

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #12


"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Willy Denoyette [MVP] <wi*************@pandora.be> wrote:
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.

Jon,

Exactly, that's why I asked "what's hidden behind the question" as I
(wrongly?) supposed OP did try the DoEvents 'hack' without the expected
result.
Also note that when running a tied loop in your handler, and this without
executing some sort of synchronous callout (PInvoke OS blocking services,
COM etc.) inside the loop, it doesn't necessarily render the UI more
responsive because you pump messages (call DoEvents) periodically.
Therefore I would suggest you only call DoEvents if you perfectly know all
implications of doing so, else you better fire off another thread to run the
handler code.

To OP, did you try DoEvents, what exactly are you doing in the handler and
how long does it take for for it to return?

Willy.
Jul 21 '05 #13
ShaneB,

He didn't say he was running a tied loop either, so we are guessing :-)
But basically we are saying the same thing.

Willy.

"ShaneB" <st********@yahoo.com> wrote in message
news:OE**************@TK2MSFTNGP12.phx.gbl...
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

"Willy Denoyette [MVP]" <wi*************@pandora.be> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
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.

"ShaneB" <st********@yahoo.com> wrote in message
news:e1*************@TK2MSFTNGP11.phx.gbl...
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

"bazad" <no****@noreply.not> wrote in message
news:1099610547.P7cij9OT9nkECcP+fXHq2w@teranews...
"Willy Denoyette [MVP]" <wi*************@pandora.be> writes:

> "bazad" <no****@noreply.not> wrote in message
> news:1099605909.kJ+tXbnQcb3AE70X6mbdMw@teranews...
> > "Willy Denoyette [MVP]" <wi*************@pandora.be> writes:
> >
> >> "bazad" <no****@noreply.not> wrote in message
> >> news:1099593932.4+jIsAHupOrTgThYXFT72g@teranews...
> >> > Hi,
> >> >
>
> > 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.

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.



Jul 21 '05 #14
Willy,

How do you block an UI from any managed code program.

When an OP is telling this you might in my opinion assume that it is blocked
because there is an event thrown that has a routine in it that takes "too"
long.

Solutions for that are to use an extra thread or application.doevents,
depending what is the impact in your program from the event where it is
happening and if the command that blocks the UI is a single command or
multiple in a loop by instance.

Just my thought,

Cor

"Willy Denoyette [MVP]" <wi*************@pandora.be>
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.

"ShaneB" <st********@yahoo.com> wrote in message
news:e1*************@TK2MSFTNGP11.phx.gbl...
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

"bazad" <no****@noreply.not> wrote in message
news:1099610547.P7cij9OT9nkECcP+fXHq2w@teranews...
"Willy Denoyette [MVP]" <wi*************@pandora.be> writes:

"bazad" <no****@noreply.not> wrote in message
news:1099605909.kJ+tXbnQcb3AE70X6mbdMw@teranews...
> "Willy Denoyette [MVP]" <wi*************@pandora.be> writes:
>
>> "bazad" <no****@noreply.not> wrote in message
>> news:1099593932.4+jIsAHupOrTgThYXFT72g@teranews...
>> > Hi,
>> >

> 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.

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.



Jul 21 '05 #15
Hi Shane,

Can you tell me how I can come as well in that future where your live.
Your datetime is something as a day before other people.

(Just to make you attent on it)

:-)

Cor

"ShaneB" <st********@yahoo.com>
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
"bazad" <no****@noreply.not> wrote in message
news:1099593932.4+jIsAHupOrTgThYXFT72g@teranews...
Hi,

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


Jul 21 '05 #16
lol...I was playing with some date calculations and in the process, I played
with the date on my machine and set it back incorrectly. I'm back in the
present now :)

The future wasn't what I thought it would be.

ShaneB

"Cor Ligthert" <no************@planet.nl> wrote in message
news:eL**************@TK2MSFTNGP10.phx.gbl...
Hi Shane,

Can you tell me how I can come as well in that future where your live.
Your datetime is something as a day before other people.

(Just to make you attent on it)

:-)

Cor

"ShaneB" <st********@yahoo.com>
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
"bazad" <no****@noreply.not> wrote in message
news:1099593932.4+jIsAHupOrTgThYXFT72g@teranews...
Hi,

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



Jul 21 '05 #17
Ah...now that is really the question: What is happening in the handler that
is taking so long?

I would tend to agree that a thread is a better option unless you can be
sure that processing messages will not cause reentrancy problems or if the
handler does get reentered, that it handles it safely.

ShaneB

"Willy Denoyette [MVP]" <wi*************@pandora.be> wrote in message
news:Oo**************@TK2MSFTNGP14.phx.gbl...


"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Willy Denoyette [MVP] <wi*************@pandora.be> wrote:
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.

Jon,

Exactly, that's why I asked "what's hidden behind the question" as I
(wrongly?) supposed OP did try the DoEvents 'hack' without the expected
result.
Also note that when running a tied loop in your handler, and this without
executing some sort of synchronous callout (PInvoke OS blocking services,
COM etc.) inside the loop, it doesn't necessarily render the UI more
responsive because you pump messages (call DoEvents) periodically.
Therefore I would suggest you only call DoEvents if you perfectly know all
implications of doing so, else you better fire off another thread to run
the handler code.

To OP, did you try DoEvents, what exactly are you doing in the handler and
how long does it take for for it to return?

Willy.

Jul 21 '05 #18
On Fri, 5 Nov 2004 10:04:22 +0100, Willy Denoyette [MVP] wrote:
Also note that when running a tied loop in your handler, and this without


Can you explain to me what a "tied loop" is? I have not seen that
expression before.

--
Chris

dunawayc[AT]sbcglobal_lunchmeat_[DOT]net

To send me an E-mail, remove the "[", "]", underscores ,lunchmeat, and
replace certain words in my E-Mail address.
Jul 21 '05 #19
"Willy Denoyette [MVP]" <wi*************@pandora.be> writes:
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Willy Denoyette [MVP] <wi*************@pandora.be> wrote:
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.

Jon,

To OP, did you try DoEvents, what exactly are you doing in the handler and
how long does it take for for it to return?


I've never used DoEvents.

It takes several seconds for the call to return.

I'd like to understand re-entrancy problems. Visual Studio warns
about re-entrancy problems, but it does not explain why or how they
occur. What can be done to prevent them.
Jul 21 '05 #20
"Willy Denoyette [MVP]" <wi*************@pandora.be> writes:
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Willy Denoyette [MVP] <wi*************@pandora.be> wrote:
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.

Jon,

To OP, did you try DoEvents, what exactly are you doing in the handler and
how long does it take for for it to return?


I've never used DoEvents.

It takes several seconds for the call to return. I'd say it is a set
of blocked calls in a loop. I execute several SQL queries which take time to
run.

I think I understand now how DoEvents works.

I'd like to make sure I understand re-entrancy problems. I assume
that if UI is responsive user can click on the same button twice.
This is the re-entrancy scenario I can come up with. My best solution
would be to make sure that button is disabled, while long time
operation is in progress.
Jul 21 '05 #21
bazad <no****@noreply.not> wrote:
To OP, did you try DoEvents, what exactly are you doing in the handler and
how long does it take for for it to return?
I've never used DoEvents.


That's a good way to keep it :)
It takes several seconds for the call to return.
Right.
I'd like to understand re-entrancy problems. Visual Studio warns
about re-entrancy problems, but it does not explain why or how they
occur. What can be done to prevent them.


Avoid using DoEvents in the first place - make the long-running call on
another thread.

Re-entrancy occurs if you call DoEvents, and something else needs to
execute, so it might end up calling back into your code again - so
you've got a second level of stack going on. You can then end up
getting into some real messes if you're not expecting it.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #22
I good example of a reentrancy problem is if you place an
Application.DoEvents call inside an Control.Paint method. The paint method
may be called a number of times and interrupted each time before it
completes. This could eventually cause a stack overflow.

ShaneB

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP***********************@msnews.microsoft.co m...
bazad <no****@noreply.not> wrote:
> To OP, did you try DoEvents, what exactly are you doing in the handler
> and
> how long does it take for for it to return?


I've never used DoEvents.


That's a good way to keep it :)
It takes several seconds for the call to return.


Right.
I'd like to understand re-entrancy problems. Visual Studio warns
about re-entrancy problems, but it does not explain why or how they
occur. What can be done to prevent them.


Avoid using DoEvents in the first place - make the long-running call on
another thread.

Re-entrancy occurs if you call DoEvents, and something else needs to
execute, so it might end up calling back into your code again - so
you've got a second level of stack going on. You can then end up
getting into some real messes if you're not expecting it.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Jul 21 '05 #23
bazad <no****@noreply.not> wrote:
"Willy Denoyette [MVP]" <wi*************@pandora.be> writes:
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
> Willy Denoyette [MVP] <wi*************@pandora.be> wrote:
>> 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.

Jon,

To OP, did you try DoEvents, what exactly are you doing in the handler and
how long does it take for for it to return?


I've never used DoEvents.

It takes several seconds for the call to return. I'd say it is a set
of blocked calls in a loop. I execute several SQL queries which take time to
run.

I think I understand now how DoEvents works.

I'd like to make sure I understand re-entrancy problems. I assume
that if UI is responsive user can click on the same button twice.
This is the re-entrancy scenario I can come up with.

My best solution
would be to make sure that button is disabled, while long time
operation is in progress.
Good idea - disable before you invoke the asynchronous call
to a delegate instance and re-enable at the end of
processing the callback (read the provided links for
details).

After all this banter shouldn't somebody have suggested
using the "BeginInvoke/EndInvoke" asynchronous functionality
on a delegate to the "time consuming" method, instead of
messing around with Application.DoEvents?

..NET Delegates: Making Asynchronous Method Calls in the .NET
Environment
http://msdn.microsoft.com/msdnmag/issues/01/08/async/

Async-Delegate Technology Sample
http://msdn.microsoft.com/library/de...ogy_sample.asp
Jon Skeet [C# MVP] <sk***@pobox.com> wrote:

bazad <no****@noreply.not> wrote:
I have a button click event handler which takes a while to execute.


In that case, it should execute on a different thread.
Application.DoEvents is a sort of band-aid solution to the problem, but
threading is a much better solution, even though it's more complicated.

See http://www.pobox.com/~skeet/csharp/t...winforms.shtml


OK so Jon did. The significance of that post got drowned out
somehow. Anyway IMO "BeginInvoke/EndInvoke" isn't that
complicated - especially considering how useful it is.

:)
Jul 21 '05 #24


"Chris Dunaway" <"dunawayc[[at]_lunchmeat_sbcglobal[dot]]net"> wrote in
message news:1v*******************************@40tude.net. ..
On Fri, 5 Nov 2004 10:04:22 +0100, Willy Denoyette [MVP] wrote:
Also note that when running a tied loop in your handler, and this without


Can you explain to me what a "tied loop" is? I have not seen that
expression before.

--
Chris

dunawayc[AT]sbcglobal_lunchmeat_[DOT]net

To send me an E-mail, remove the "[", "]", underscores ,lunchmeat, and
replace certain words in my E-Mail address.


I'm not sure it's a valid expression, but I'm using it here to indicate that
the CPU resource is tied up in a tight finite loop, which appears difficult
to break.

Willy.
Jul 21 '05 #25

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Ollie Riches | last post by:
I understand the use of Application.DoEvents() to process all outstanding messages on the message queue in a winforms application if you have long running process on the UI thread. But can anyone...
23
by: bazad | last post by:
Hi, I'd like to understand consequences of Application.DoEvents call. Does it create a new thread? Thank you
13
by: Amjad | last post by:
Hi, Is there an equivalent to the "Application.Doevents" method in modules or Windows services? I want to make a Windows service that calls a DLL. The DLL would have all my functions and it...
11
by: Don | last post by:
I have a WPF application in VB in VSTS 2008 RTM. I am trying to "blink" (momentarily clear) a field of data if the data is reloaded from the database to give the user some visual indication of the...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.