+ Reply to Thread
Results 1 to 2 of 2

Thread: Notify User about finished Process

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    31

    Notify User about finished Process

    I have a process running on the background and i would like to notify the user when the process is finished , i`d like to display a message to the user , the best place to display this would be the masterpage AFAIK (incase they move from page to page on the site) so i have defined a div in the masterpage file.

    I have created a method to do that , but the problem is that it is not dynamic and i have to refresh the page so the message can appear and i`d like to use AJAX to send the message every 5 seconds .

    I have tried using an asp timer with a 5 second tick time but the entire page refreshes on the timer click event every 5 seconds.
    Code:
    Protected Sub Timer1_Tick(Byval sender as Object, Byval e As EventArgs)
        poll_results()
    End Sub
    
    Public Sub poll_results()
    
     'database access logic 
    
     If dbtable.isCompleted = True
         Dim sb As New StringBuilder()
         sb.Append("$('#status').html('Job abc123 is complete');")
         Page.ClientScript.RegisterStartupScript(GetType(Page), "myscript", sb.ToString(), True)
     End If
    
    End Sub
    Is there a way to only refresh the div with the updated message, without a full refresh happening using this code?

  2. #2
    Junior Member
    Join Date
    Jan 2012
    Posts
    20
    In ASP there is something called async call , instead calling your method in the same page, just create an async folder in your website and add a new Generic Handler, call the page, for example checkPendingJobs.ashx.
    and add the below code inside that page : [configure server side]
    Code:
    Public Sub ProcessRequest (Byval context as HttpContext)
    
        Dim r As String = ""
    
        If dbtable.isCompleted = True
           Dim sb As New StringBuilder()
           r = "Job #216 is complete"
        End If     
    
        context.Response.ContentType = "text/plain"
        context.Response.Write(r)
    }
    Now add this jQuery code to tour masterpage, witch will fire every time some one refreshes the page, or you can use setInterval() to check it every xx seconds:[Clients side]
    Code:
    <script>
    
    var checkJobs = true;
    
    $(document).ready(function() {
    
        // every time someone refreshes the page
        checkPendingJobs();
    
        // every 5 seconds...
      setInterval(function(){ checkPendingJobs(); }, 5000);
    
    });
    
    function checkPendingJobs() {
        // no need to check it if we already have a valid response
        if(!checkJobs) return;
    
        // caching issue trick
        var now = new Date(); 
    
        $.ajax({
            url: "../async/checkPendingJobs.ashx",
            type: "GET",
            data: { t: now.getTime() },
            success: function(data) {
    
                // do we have text to show?
                if(data.length > 0) {
                    alert(data);
                    checkJobs = false; // no need to check it more...
                    // $('#status').html(data);
                }
            },
            error: function(msg) {
                alert('Error found:\n' + msg);
            }
        });        
    }
    
    </script>

+ Reply to Thread

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts