473,387 Members | 1,517 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,387 software developers and data experts.

DoEvents() "Hangs"

Group:

We have an application that is calling a stored proc. The stored proc
takes anywhere from 15 to 90 minutes to run.

In order to keep the GUI responsive, we are using a BackgroundWorker to
actually make the call to the stored proc. The app then sits in a loop
that awakes every second and updates an elapsed time value.

The problem that we are having is that after a certain period of time -
which is rarely the same amount - the call to DoEvents hangs (does not
return).

I was hoping the group could help us with: a) why thiat might be
occuring and b) what if anything can be done to fix it.

Here's the key areas of the code:

This is from the method that starts the ball rolling:
Aug 3 '06 #1
15 5947
Hello ce******@ocv.com,

Do people really still use DoEvents() (is it even valid in C#?). You should
be using Thread.Join or Thread.Sleep (or a synchronization semantic like
a Mutex or Monitor).
Thanks,
Shawn Wildermuth
Speaker, Author and C# MVP
http://adoguy.com
Group:

We have an application that is calling a stored proc. The stored proc
takes anywhere from 15 to 90 minutes to run.

In order to keep the GUI responsive, we are using a BackgroundWorker
to actually make the call to the stored proc. The app then sits in a
loop that awakes every second and updates an elapsed time value.

The problem that we are having is that after a certain period of time
- which is rarely the same amount - the call to DoEvents hangs (does
not return).

I was hoping the group could help us with: a) why thiat might be
occuring and b) what if anything can be done to fix it.

Here's the key areas of the code:

This is from the method that starts the ball rolling:
.
.
.
case "Consolidate Revenue": {
bgwThread.RunWorkerAsync( cConsolidateRevenue );
SleepAndUpdate();
break;
}
.
.
.
This is from the "DoWork" event handler:

private void bgwThread_DoWork( object sender, DoWorkEventArgs e )
{
switch ((int)e.Argument) {
case cConsolidateRevenue: {
ConsolidateRevenue( cn, dtPickerStartDate.Value.Date,
dateImportEndDate );
break;
}
.
.
.
Here is SleepAndUpdate:
private void SleepAndUpdate( ) {
while (notDone) {
ShowElapsedTime();
Application.DoEvents();
System.Threading.Thread.Sleep( 1000 );
}
}
We have verfied that the app is indeed haning on the call to
DoEvents()

Thank you for your help

Aug 3 '06 #2
Why are you having the front end stop if you are running the operation on a
background thread anyways?

Couldn't you just make the background thread tell the front end when it is
done through a callback/event/etc

Cheers,

Greg Young
MVP - C#
http://codebetter.com/blogs/gregyoung

<ce******@ocv.comwrote in message
news:11**********************@75g2000cwc.googlegro ups.com...
Group:

We have an application that is calling a stored proc. The stored proc
takes anywhere from 15 to 90 minutes to run.

In order to keep the GUI responsive, we are using a BackgroundWorker to
actually make the call to the stored proc. The app then sits in a loop
that awakes every second and updates an elapsed time value.

The problem that we are having is that after a certain period of time -
which is rarely the same amount - the call to DoEvents hangs (does not
return).

I was hoping the group could help us with: a) why thiat might be
occuring and b) what if anything can be done to fix it.

Here's the key areas of the code:

This is from the method that starts the ball rolling:
.
.
.
case "Consolidate Revenue": {
bgwThread.RunWorkerAsync( cConsolidateRevenue );
SleepAndUpdate();
break;
}
.
.
.

This is from the "DoWork" event handler:

private void bgwThread_DoWork( object sender, DoWorkEventArgs e ) {
switch ((int)e.Argument) {
case cConsolidateRevenue: {
ConsolidateRevenue( cn, dtPickerStartDate.Value.Date,
dateImportEndDate );
break;
}
.
.
.

Here is SleepAndUpdate:
private void SleepAndUpdate( ) {
while (notDone) {
ShowElapsedTime();
Application.DoEvents();
System.Threading.Thread.Sleep( 1000 );
}
}
We have verfied that the app is indeed haning on the call to DoEvents()

Thank you for your help

Aug 3 '06 #3
The business requirements are such that the call needs to be
synchronous but the GUI needs to be semi-responsive while it is
running.

The users are not able to perform any other taks in the app while the
call is being processed but when the process is long running they
don't want the screen to "white" out either.

That is the only reason for the threading.

Aug 3 '06 #4
Then you need to just disable the GUI programmitically. Thread.Sleep()
on the UI thread is never correct. Look into using the BackgroundWorker
class for a simple and sound solution if you're using 2.0.

http://msdn2.microsoft.com/en-us/lib...undworker.aspx

ce******@ocv.com wrote:
The business requirements are such that the call needs to be
synchronous but the GUI needs to be semi-responsive while it is
running.

The users are not able to perform any other taks in the app while the
call is being processed but when the process is long running they
don't want the screen to "white" out either.

That is the only reason for the threading.
Aug 3 '06 #5
Exactly, I am using the BackgroundWorker (see code snippets) above. In
essence, I am firing off the stored proc and then putting the GUI in a
loop that sleeps for a while, awakens, and updates itself and then goes
back to sleep. This continues until the stored proc returns.

But the trouble is that it (the SleepAndUpdate loop) stops working
after a while. When it first fires up, everything is great and then
after an indeterminate period of time the DoEvents() stops returning.

It is puzzling.

I hate to take the "easy" way out and point to the framework but that
is indeed what it seems like the trouble is.

All helpful thoughts and suggestions (not the snarky ones about "Do
people really still use DoEvents() (is it even valid in C#?).") are
appreciated.

wf****@gmail.com wrote:
Then you need to just disable the GUI programmitically. Thread.Sleep()
on the UI thread is never correct. Look into using the BackgroundWorker
class for a simple and sound solution if you're using 2.0.

http://msdn2.microsoft.com/en-us/lib...undworker.aspx
Aug 3 '06 #6
Don't sleep on the UI thread. Ever. You're preventing the UI
thread from processing any messages for 1 second. Disable the main form
and its controls or launch another simple modal form to actually launch
the processing thread and display some sort of "I'm running" indicator
until it receives notification (not by polling, by callback) that the
thread has finished.
Your implementation is broken, not the framework. This is not meant
to be snarky, just straightforward.

ce******@ocv.com wrote:
Exactly, I am using the BackgroundWorker (see code snippets) above. In
essence, I am firing off the stored proc and then putting the GUI in a
loop that sleeps for a while, awakens, and updates itself and then goes
back to sleep. This continues until the stored proc returns.

But the trouble is that it (the SleepAndUpdate loop) stops working
after a while. When it first fires up, everything is great and then
after an indeterminate period of time the DoEvents() stops returning.

It is puzzling.

I hate to take the "easy" way out and point to the framework but that
is indeed what it seems like the trouble is.

All helpful thoughts and suggestions (not the snarky ones about "Do
people really still use DoEvents() (is it even valid in C#?).") are
appreciated.

wf****@gmail.com wrote:
Then you need to just disable the GUI programmitically. Thread.Sleep()
on the UI thread is never correct. Look into using the BackgroundWorker
class for a simple and sound solution if you're using 2.0.

http://msdn2.microsoft.com/en-us/lib...undworker.aspx
Aug 3 '06 #7
wfa:

Sorry, I wasn't indicting you. I just get frustrated with condescending
posts.

In any case, the issue is that when the call returns then we must move
onto the next step in the process. We let the stored proc run in the
back ground BUT we can't continue on with processing until the stored
proc returns. That is why we go into a sleep loop. Otherwise, we just
loop until the semaphore is reset.

wfa...@gmail.com wrote:
Don't sleep on the UI thread. Ever. You're preventing the UI
thread from processing any messages for 1 second. Disable the main form
and its controls or launch another simple modal form to actually launch
the processing thread and display some sort of "I'm running" indicator
until it receives notification (not by polling, by callback) that the
thread has finished.
Your implementation is broken, not the framework. This is not meant
to be snarky, just straightforward.
Aug 3 '06 #8
I agree with this completely.

You can just leave the UI doing nothing and wait on an event from the worker
thread (i.e. with a modal dialog). This is a quite common task (and many
people include "cancel" buttons on such a screen so the user can stop what
they are doing).

Personally I would also question the business requirement of having a user
on the system unable to use their interface for 15-90 minutes as this sounds
exactly like the type of job that should be done by some system service. By
putting the requirement that the person can't do anything when this is
happenning also leads directly to the conclusion that noone can be doing
anything while this happens on the database (or atleast touching the same
data).

Cheers,

Greg

<wf****@gmail.comwrote in message
news:11**********************@i42g2000cwa.googlegr oups.com...
Don't sleep on the UI thread. Ever. You're preventing the UI
thread from processing any messages for 1 second. Disable the main form
and its controls or launch another simple modal form to actually launch
the processing thread and display some sort of "I'm running" indicator
until it receives notification (not by polling, by callback) that the
thread has finished.
Your implementation is broken, not the framework. This is not meant
to be snarky, just straightforward.

ce******@ocv.com wrote:
>Exactly, I am using the BackgroundWorker (see code snippets) above. In
essence, I am firing off the stored proc and then putting the GUI in a
loop that sleeps for a while, awakens, and updates itself and then goes
back to sleep. This continues until the stored proc returns.

But the trouble is that it (the SleepAndUpdate loop) stops working
after a while. When it first fires up, everything is great and then
after an indeterminate period of time the DoEvents() stops returning.

It is puzzling.

I hate to take the "easy" way out and point to the framework but that
is indeed what it seems like the trouble is.

All helpful thoughts and suggestions (not the snarky ones about "Do
people really still use DoEvents() (is it even valid in C#?).") are
appreciated.

wf****@gmail.com wrote:
Then you need to just disable the GUI programmitically. Thread.Sleep()
on the UI thread is never correct. Look into using the BackgroundWorker
class for a simple and sound solution if you're using 2.0.

http://msdn2.microsoft.com/en-us/lib...undworker.aspx

Aug 3 '06 #9
Have the background thread notify the foreground thread when it is complete
.....

Also you can't continue with the process involving the sproc but can you do
other things while waiting or is the app designed to only do this one
process and the one process doesn't make sense to be doing multiples (i.e.
running a big report).

Cheers,

Greg
<ce******@ocv.comwrote in message
news:11*********************@m73g2000cwd.googlegro ups.com...
wfa:

Sorry, I wasn't indicting you. I just get frustrated with condescending
posts.

In any case, the issue is that when the call returns then we must move
onto the next step in the process. We let the stored proc run in the
back ground BUT we can't continue on with processing until the stored
proc returns. That is why we go into a sleep loop. Otherwise, we just
loop until the semaphore is reset.

wfa...@gmail.com wrote:
>Don't sleep on the UI thread. Ever. You're preventing the UI
thread from processing any messages for 1 second. Disable the main form
and its controls or launch another simple modal form to actually launch
the processing thread and display some sort of "I'm running" indicator
until it receives notification (not by polling, by callback) that the
thread has finished.
Your implementation is broken, not the framework. This is not meant
to be snarky, just straightforward.

Aug 3 '06 #10
Yes, well, while I won't defend the architecture, we are constrained to
work within it.

I did try removing the Sleep from the wait loop. Oddly enough, this
caused the DoEvents to hang sooner. This led us to make the sleep
longer which seems to make the situation better though the hanging
still occurs - just much later in the process.

Using a Modal dialog box is a solution but we are still faced with the
GUI not responding to simple redraw messages.

We believe that will the current solution is ugly it should still work.
We remain puzzled that the DoEvents will work but then suddenly stop
responding.

Greg Young wrote:
I agree with this completely.

You can just leave the UI doing nothing and wait on an event from the worker
thread (i.e. with a modal dialog). This is a quite common task (and many
people include "cancel" buttons on such a screen so the user can stop what
they are doing).

Personally I would also question the business requirement of having a user
on the system unable to use their interface for 15-90 minutes as this sounds
exactly like the type of job that should be done by some system service. By
putting the requirement that the person can't do anything when this is
happenning also leads directly to the conclusion that noone can be doing
anything while this happens on the database (or atleast touching the same
data).

Cheers,

Greg
Aug 3 '06 #11
Note that I said to use a modal dialog and an event/call back.

You would start the thread .... then just sit and do nothing (just show your
screen maybe with a cancel button). There is no loop (well there is but its
not in your code). Later your thread will raise the event or directly call
you back at which point you continue your processing.

Cheers,

Greg
<ce******@ocv.comwrote in message
news:11**********************@i3g2000cwc.googlegro ups.com...
Yes, well, while I won't defend the architecture, we are constrained to
work within it.

I did try removing the Sleep from the wait loop. Oddly enough, this
caused the DoEvents to hang sooner. This led us to make the sleep
longer which seems to make the situation better though the hanging
still occurs - just much later in the process.

Using a Modal dialog box is a solution but we are still faced with the
GUI not responding to simple redraw messages.

We believe that will the current solution is ugly it should still work.
We remain puzzled that the DoEvents will work but then suddenly stop
responding.

Greg Young wrote:
>I agree with this completely.

You can just leave the UI doing nothing and wait on an event from the
worker
thread (i.e. with a modal dialog). This is a quite common task (and many
people include "cancel" buttons on such a screen so the user can stop
what
they are doing).

Personally I would also question the business requirement of having a
user
on the system unable to use their interface for 15-90 minutes as this
sounds
exactly like the type of job that should be done by some system service.
By
putting the requirement that the person can't do anything when this is
happenning also leads directly to the conclusion that noone can be doing
anything while this happens on the database (or atleast touching the same
data).

Cheers,

Greg

Aug 3 '06 #12
Well you are constrained to the architecture but it is up to you as the
engineers to build a solid implementation. We are not trying to offend
you (or whoever designed this originally and now seems married to it)
but we're trying to get the point across that your current design is
broken. Get rid of DoEvents and get rid of Sleep, you don't need either
one of them. It will only work if you're lucky, the solutions we're
pushing will work reliably. Don't sit there puzzling over it for
another minute. Take 30 minutes and rewrite it correctly.

cedmu...@ocv.com wrote:
Yes, well, while I won't defend the architecture, we are constrained to
work within it.

I did try removing the Sleep from the wait loop. Oddly enough, this
caused the DoEvents to hang sooner. This led us to make the sleep
longer which seems to make the situation better though the hanging
still occurs - just much later in the process.

Using a Modal dialog box is a solution but we are still faced with the
GUI not responding to simple redraw messages.

We believe that will the current solution is ugly it should still work.
We remain puzzled that the DoEvents will work but then suddenly stop
responding.

Greg Young wrote:
I agree with this completely.

You can just leave the UI doing nothing and wait on an event from the worker
thread (i.e. with a modal dialog). This is a quite common task (and many
people include "cancel" buttons on such a screen so the user can stop what
they are doing).

Personally I would also question the business requirement of having a user
on the system unable to use their interface for 15-90 minutes as this sounds
exactly like the type of job that should be done by some system service. By
putting the requirement that the person can't do anything when this is
happenning also leads directly to the conclusion that noone can be doing
anything while this happens on the database (or atleast touching the same
data).

Cheers,

Greg
Aug 3 '06 #13
Hi,
We believe that will the current solution is ugly it should still work.
We remain puzzled that the DoEvents will work but then suddenly stop
responding.
Well I hope you take some of the very good advice you have been given so
far, but I must admit I do agree with your statement here. Hangs like this
can be caused by deadlocks though you haven't shown enough code to tell us
much there. I also wonder if your thread code might be poking its nose into
UI components in a non thread safe way?

Cheers
Doug Forster
Aug 4 '06 #14
Doug Forster wrote:
Hi,
>We believe that will the current solution is ugly it should still
work. We remain puzzled that the DoEvents will work but then
suddenly stop responding.

Well I hope you take some of the very good advice you have been given
so far, but I must admit I do agree with your statement here. Hangs
like this can be caused by deadlocks though you haven't shown enough
code to tell us much there. I also wonder if your thread code might
be poking its nose into UI components in a non thread safe way?
....which is to say, in any way at all.

My first thought on reading the initial post in this thread was that the
background thread is trying to update the UI without going through
Control.{Begin}Invoke - that's a sure recipe for UI hangs like this. But
since he's apparaently using .NET 2.0, I doubt that's the case, since the
2.0 Winforms classes will throw an exception if you touch them from the
wrong thread.

I agree completely with the other advice in this thread: get rid of the
sleep/doEvents loop and put up a modal form, or just disable the main form
until a callback (or event) from the worker thread indicates that it's time
to move on to the next step.

-cd


Aug 4 '06 #15
On 3 Aug 2006 13:36:40 -0700, ce******@ocv.com wrote:
>All helpful thoughts and suggestions (not the snarky ones about "Do
people really still use DoEvents() (is it even valid in C#?).") are
appreciated.
That may have been snarky but it was absolutely correct.
Do not use DoEvents, ever! There is never a good reason to have the
operating system perform unpredictable reentrant calls on the current
GUI thread -- because that's exactly what DoEvents does.
--
http://www.kynosarges.de
Aug 4 '06 #16

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

Similar topics

0
by: Roger Espinosa | last post by:
I'm attempting to install dbxml-1.10 on OS X 10.2.8 (gcc3.x prerelease, Python 2.3.2 built on same), and while everything builds happily, when I try to invoke import dbxml Python just ......
1
by: Vivien Mallet | last post by:
Hello, I use popen2.Popen4 and I experienced problems with it. Let me show you with an example. The following script is called "lines" (it prints lines of 'X'): ---------------------...
0
by: David H | last post by:
Background. I'm running on WinXP w/ MS Services for Unix installed (to give rsh/rlogin ability), both Python 2.3 and 2.4 version. In linux, I'm running RHEE with python2.3 version. The code...
1
by: szudor | last post by:
Hi, When I start db2cc, the program hangs with memory access violation in javaw.exe. Strating with trace option, in java trace file I found the following information: 0SECTION TITLE...
3
by: Johnny M | last post by:
using Access 2003 Pardon the subject line, but I don't have a better word for this strange behavior (or behavior I don't understand!!!) I have a class module named DepreciationFactor. One of...
1
by: happyinst | last post by:
I have recently added some code to a web application and now I find it hanging after I do about 2 or 3 postbacks within the application. It hangs after doing different things (I cannot find...
3
by: Wardeaux | last post by:
All, I've written a "setup" wrapper that calls a sequence of "setup.exe", and all works except when I call the setup.exe for the MSDE, then it gets about half way through and then hangs... The...
4
by: dumbkiwi | last post by:
I have written a script that uses the urllib2 module to download web pages for parsing. If there is no network interface, urllib2 hangs for a very long time before it raises an exception. I...
5
by: =?Utf-8?B?UmljaA==?= | last post by:
Greetings, I have been using the Click Once deployment feature of VS2005 - very nice. I had IE6 at the time when I started using Click Once Deployment, and everything worked fine. But then my...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.