Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / March 2008

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Kristijan Marin - 19 Mar 2008 10:59 GMT
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 - 20 Mar 2008 17:30 GMT
> [...]
> What I did put in the code to force refresh of other processes is :
>
> Application.DoEvents();
> Thread.Sleep(300);

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.

Pete

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.