Visual Basic.NET
  Home arrow Visual Basic.NET arrow Page 3 - Developing Long Running Tasks Using Asynch...
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Dedicated Servers 
Actuate Whitepapers 
Moblin 
JMSL Numerical Library 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
VISUAL BASIC.NET

Developing Long Running Tasks Using Asynchronous Programming with VB.NET 2005
By: Jagadish Chaterjee
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 15
    2006-07-31

    Table of Contents:
  • Developing Long Running Tasks Using Asynchronous Programming with VB.NET 2005
  • What exactly is the problem?
  • The solution is here
  • The asynchronous way of dealing with a long task

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT

    Ajax Application Generator Generate database and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!

    Developing Long Running Tasks Using Asynchronous Programming with VB.NET 2005 - The solution is here


    (Page 3 of 4 )

    In the previous section, I showed you the problem, but didn’t give you a solution. Before giving you the solution, you need to understand the application execution a bit more in depth.

    When an application is executed, it starts its own thread of execution (generally called the main/application thread). The application is closed when the thread is ended.  Nevertheless, of any of number of instructions you embed within the application, all are executed in the same thread, one by one, in a hierarchical manner (sometimes even in the form of a top-down approach to execution).

    If our application is trying to complete all the instructions in a task, it does so with a hell of a lot of speed. With this speed, it would not even find time to update the screen (even if we ask it to do so)! 

    The simplest solution is including a new simple statement as follows:

        Private Sub ExecuteTask()
            For i As Long = 1 To 100000
                Me.lblMessage.Text = i
                Application.DoEvents()
            Next
        End Sub

    You can observe the statement “Application.DoEvents.” It really does the magic.  Now, our application responds to the user in a professional manner. What exactly does this new statement do? During the execution of several instructions of a process/task, that simple statement pauses the application thread for a moment to handle user events (like updating the screen, handling mouse events, and so on).

    Until now, it is quite perfect. The above method works quite well if you are working with several instructions. Let us consider the possibility that you are working on only one instruction from your application's point of view. The best example of this is the instruction “check for database connectivity.” It is really a simple instruction to check for the existence of a database. Now, the task's delay is not from our application. It is from an external source. Our application will not respond, unless the current instruction is completed (even though you use Application.Events). You cannot even close your application properly if it is frozen or unresponsive.

    The second method (which I consider as the best) involves using a separate thread for the process and executing the same thread asynchronously with call backs for better presentation. 

    More Visual Basic.NET Articles
    More By Jagadish Chaterjee


       · hello guys. This contribution introduces you to work with long running tasks. enjoy...
       · I have code in which i need to enable a cancel button during a process.Right now i...
       · In Vb, you can use the same code that is called when the task starts to make sure it...
       · If you don't add "application.DoEvents" to the ExecuteTask Sub, the system will hang...
     

    VISUAL BASIC.NET ARTICLES

    - Entity Creation and Messaging in a VB.NET Te...
    - Movement and Player Statistics in a VB.NET T...
    - Creating and Drawing a Game Map in VB.NET
    - Working with Classes and Properties for Game...
    - Working with Loops, Arrays, and Collections ...
    - Learning Loops in VB.NET for Game Development
    - Learning VB.NET: Working with Variables, Con...
    - The Basics of VB.NET Through Text Game Devel...
    - Learning VB.NET Through Text Game Development
    - Types of Operators in Visual Basic
    - Operators
    - Understanding Custom Events using Visual Bas...
    - Polymorphism using Abstract Classes in Visua...
    - Shadowing using Shadows in Visual Basic.NET ...
    - Overloading and Overriding in Visual Basic.N...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway