fire Client Side code from Server Side at runtime

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • yarbrough40
    Contributor
    • Jun 2009
    • 320

    fire Client Side code from Server Side at runtime

    alright now I know this defeats the purpose of client side vs. server side (refer to title of this thread)... What I would love to do is build my own custom progress indicator on the client showing how far a server side code has progressed. I figure if I could only get a JavaScript to fire and render a value of some control at pre-defined intervals while the server is working I could make it happen - but alas I'm beginning to believe that this is impossible. Below is some code that performs three 2 sec loops. At the end of each I want to change the visible value of a control on the browser.

    This is a fun one! I'm beginning to believe that it isn't even possible.... If anyone has any ideas I would LOVE to hear them!


    Code:
     Dim dt As Date = Now()
            Dim dt2 As Date = dt.AddSeconds(2)
            Do Until Now() = dt2
            Loop
            Response.Write("first Loop complete<br />") '(does not work - waits till postback has finished and all three responses show)
            ''FIRE SOME ALREADY REGESTERED JAVASCRIPT FUNC()
    
            dt = Now()
            dt2 = dt.AddSeconds(2)
            Do Until Now() = dt2
            Loop
            Response.Write("second Loop complete <br />") '(does not work - waits till postback has finished and all three responses show)
            ''FIRE SOME ALREADY REGESTERED JAVASCRIPT FUNC()
    
            dt = Now()
            dt2 = dt.AddSeconds(2)
            Do Until Now() = dt2
            Loop
            Response.Write("third Loop complete <br />") '(does not work - waits till postback has finished and all three responses show)
            ''FIRE SOME ALREADY REGESTERED JAVASCRIPT FUNC()
  • Aimee Bailey
    Recognized Expert New Member
    • Apr 2010
    • 197

    #2
    The easiest way to do this is to save a file on the server, then have your javascript read that file every so often to see how far its got.

    I have used this method before, except my server included apache http server so i could read the progress anyware in the world. Note, id stay away from straight flat files, instead use something like xml to give your file a format thats easily readable by javascript, heres an example of writing an xml file in vb.net:

    Code:
    Imports System.Xml
    
        'The following sub formats and saves an xml file.
    
        Public Sub PublishProgress(ByVal file As String, _
                                   ByVal value As Integer, _
                                   ByVal maxVal As Integer)
            Try
                Dim iDoc As New XmlDocument()
    
                Dim nodeMain As XmlNode = iDoc.CreateElement("progress")
                Dim nodeVal As XmlNode = iDoc.CreateElement("value")
                Dim nodeMax As XmlNode = iDoc.CreateElement("max")
    
                nodeVal.InnerText = value
                nodeMax.InnerText = maxVal
    
                nodeMain.AppendChild(nodeVal)
                nodeMain.AppendChild(nodeMax)
    
                iDoc.AppendChild(nodeMain)
                iDoc.Save(file)
    
            Catch ex As XmlException
                MessageBox.Show(ex.Message & vbCrLf & ex.StackTrace)
            End Try
        End Sub
    You would call something like this everytime the progress changed on your server application, and the client would read that file every so often to see if you had updated it.

    Its worth a note that the clue to things like this is in the name "Client" and "Server", unless you have a async data system setup, you'll always have the server providing data, and the client asking weather its available.

    Hope this helps!

    Aimee.

    Comment

    • yarbrough40
      Contributor
      • Jun 2009
      • 320

      #3
      This is exactly the kind of thing I was looking for! totally get it..... I'm on it. Great insight!

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        If this is an ASP.NET question you may also be interested in checking out this thread: ASP.NET Progress.

        -Frinny

        Comment

        • yarbrough40
          Contributor
          • Jun 2009
          • 320

          #5
          Frinny, That is indeed an interesting conversation u had on the subject. I have never used Atlas controls. Is there any issue that you know of using Atlas and Ajax in the same form considering that you will then have two script managers?

          Comment

          • Frinavale
            Recognized Expert Expert
            • Oct 2006
            • 9749

            #6
            You probably shouldn't be using Atlas stuff at this stage.
            Atlas was the pre-release/beta stuff that is Ajax.NET

            I don't know of any issues but I don't think I'd recommend it either.

            -Frinny

            Comment

            • yarbrough40
              Contributor
              • Jun 2009
              • 320

              #7
              got ya! thanks... also - I think this XML route is a good one because it allows the server to quickly set the node value then move on while the browser constantly checks the node independantly. The only issue I could possibly see with this is if you have multiple users running this at the same time posing concurrency issues in the XML. The easy solution is to create a new XML doc each time then blow it away when the procedure finishes. The only issue then that I see is if you have a giant application with a boat load of concurrent users which could potentially create thousands of temporary XML docs that strain the server. Do you have any thoughts?

              Comment

              • Frinavale
                Recognized Expert Expert
                • Oct 2006
                • 9749

                #8
                You're still going to need to use Ajax to make the request either way.

                I liked the Session variable approach because a whole file seems overkill to me...and then you have to worry about cleaning up files which sometimes can be a problem if your application is recycled in the middle of processing (leaving files that will never get cleaned up).

                It looks like threading did the trick.
                I'd return to my code that I posted and try adding in threading but right now I can't...

                -Frinny

                Comment

                • yarbrough40
                  Contributor
                  • Jun 2009
                  • 320

                  #9
                  ok so the XML approach works but I agree with you that using a file is a clunky way of doing this. Interestingly enough when I attempt to set a session variable from within a background thread I get errors telling me that I need to enable session state in the page (which I did indeed do). I also made sure to add this to the web.config

                  Code:
                  <httpModules>
                  <add name="Session" type="System.Web.SessionState.SessionStateModule"/>
                  </httpModules>
                  still no dice... this continues to be a pickle!

                  Comment

                  Working...