Groups | Blog | Home
all groups > dotnet framework > march 2008 >

dotnet framework : Multi calls of same method or could Application.DoEvents messup code execution ??


Kristijan Marin
3/19/2008 10:59:34 AM
Hi,

I have experianced same wierd behavior by one of my customers .....

Here is the thing .... We have the application for newspaper article
management. so adding end editing etc ....

After a while my customer started to complain that they have duplicate
articles in there database ....

so I put some traces on the save event ..... and what I could find out is
that the event method OnClick for save ...

was called 2 or even 3 times in a row ....??? How is this possible ?? What
is even more wierd is that this happens maybe 1 or 2 times from 2500 !!!

I also put some traces when entering the method and exiting the method .....
and I get 2 times enter 1 time exit ?:???

So there can't be error in code .... Ofcourse I COUDN'T repeat this error
not even ones ....

Now the only difference from my PC to customers is that I have .NET
Framework 2.0 SP1 installed and they don't .....

What I did put in the code to force refresh of other processes is :

Application.DoEvents();
Thread.Sleep(300);

Was this wrong ?? Could this mess-up the code execution ??


Another thing that happened was when they tried to edit the article and same
it .... and because they changed the media code .... thumb generation
mechanizem puts
new line in oracle table so that the background process creates new thumb
.....

And what happened was that the code (see bellow) was somehow skiped .....
well I can't see if it was skipped or not, but there was 100% no new line
for thumb generation

void OnClick(...)
{
// some adding and updating of the article
Application.DoEvents();
Thread.Sleep(300);

if (old_media != new_media)
{
///do our table insert for thumbs .....
}
}

So as you can see i called this :
Application.DoEvents();
Thread.Sleep(300);

ones more here in this code .... so maybe this is a problem.....

Would anyone have any clue what is wrong here .

Thanks a lot.
Kris






Peter Duniho
3/20/2008 9:30:29 AM
On Wed, 19 Mar 2008 02:59:34 -0700, Kristijan Marin
[quoted text, click to view]

It is impossible to say based on how little you've posted whether that is
in fact causing your problem.

But you should certainly remove it and fix your design to work without it,
as calling DoEvents() certainly _can_ cause, or at least be related to,
problems such as what you've noticed, among other things as well.

Frankly, a call to DoEvents() does not belong in any well-written
program. If you start with bad code, it may sometimes be the most direct
way to work around a problem, but it's always better to solve the problem
(which is usually that of trying to run concurrent tasks) correctly
instead of calling DoEvents().

The main GUI thread should be used only for the user interface, and for
relatively short tasks (i.e. ones that the user will not perceive as
interfering with the user interface). In some very limited scenarios, you
may go ahead and block the GUI thread, but you should do so understanding
that the user won't be able to interact with the UI. Otherwise, use one
of the several multi-threading classes available in .NET to put the longer
tasks on a thread other than your GUI thread.

If you introduce DoEvents(), then now you have ways for the main thread to
reenter code that's already running, which can cause a variety of problems
including the "multi-click" problem you're seeing. Normally this would
only happen as a result of user input, but it's not the only way, and in
any case it sounds like you haven't ruled out reentrant user input as a
cause of the problem.

You can address some of these reentrancy problems, but it's not going to
be any easier to do that than to handle the synchronization issues that
might come up with a multi-threaded solution. If you're going to write
code that's more complicated, you might as well write _correct_
complicated code instead of incorrect complicated code.

AddThis Social Bookmark Button