Results 1 to 14 of 14

Thread: Thread.Sleep or Timer in a web service - which is proper??

  1. #1

    Thread Starter
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Thread.Sleep or Timer in a web service - which is proper??

    I'm curious which of these is a better solution - I need to pause and poll a SQL table - for a short time - in a web service.

    Thread.Sleep appears to be evil...

    But is a timer a better choice?

    Discussion is in this thread if you want to see how Penegate and I got to this point...

    http://www.vbforums.com/showthread.p...31#post4129631

    From around post 7 down...

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  2. #2
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Thread.Sleep or Timer in a web service - which is proper??

    Hello,

    I would agree that generally, the use of Thread.Sleep, is evil.

    On a wider note though, why exactly do you need to "pause". I don't understand the requirement, or the use case for doing this.

    Gary

  3. #3

    Thread Starter
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Thread.Sleep or Timer in a web service - which is proper??

    In a strict ajax post/response to a web service you get the response immediately.

    I've got a setup where a report is produced by a .bat file running on the server - it's Process.Start'd by another web service.

    When that report process completes it sets a value in a table saying that report 000123 is now done.

    I want the browser to know this fact as quickly as possible.

    Most reports run quickly - under 60 seconds - but some can take several minutes.

    I was hoping that the first AJAX call to see if the report was done would actually look for a while - up to a minute maybe - with checks to the DB occuring every 250 ms maybe. That way the instant (at least 250ms instant) the DB knows the report is done the response to the client happens...

    If that first AJAX call did not see that the report was done - the browser would then wait a bit (1000 ms at this time) and then re-send an AJAX call to the server for status update.

    This would happen over and over again until said report is completed...

    It is actually working with the code I posted in that other thread. I just don't know how scaleable this is going to be.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  4. #4
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Thread.Sleep or Timer in a web service - which is proper??

    Hello,

    For what you have described, I would be more looking to use something like an open WebSocket, rather than any form of polling.

    http://www.html5rocks.com/en/tutoria...ockets/basics/

    Gary

  5. #5
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: Thread.Sleep or Timer in a web service - which is proper??

    Quote Originally Posted by szlamany View Post
    In a strict ajax post/response to a web service you get the response immediately.

    I've got a setup where a report is produced by a .bat file running on the server - it's Process.Start'd by another web service.

    When that report process completes it sets a value in a table saying that report 000123 is now done.

    I want the browser to know this fact as quickly as possible.

    Most reports run quickly - under 60 seconds - but some can take several minutes.

    I was hoping that the first AJAX call to see if the report was done would actually look for a while - up to a minute maybe - with checks to the DB occuring every 250 ms maybe. That way the instant (at least 250ms instant) the DB knows the report is done the response to the client happens...

    If that first AJAX call did not see that the report was done - the browser would then wait a bit (1000 ms at this time) and then re-send an AJAX call to the server for status update.

    This would happen over and over again until said report is completed...

    It is actually working with the code I posted in that other thread. I just don't know how scaleable this is going to be.



    Where do you start the Process.Start method. Is it inside the webservice call method ?

    If yes then wait for the batch file close using

    Process.Start("FileNamewithpath").WaitForExit Method
    Please mark you thread resolved using the Thread Tools as shown

  6. #6

    Thread Starter
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Thread.Sleep or Timer in a web service - which is proper??

    I was under the impression that holding a web service for a long time - like the 4 minutes a report might take to complete - is not scaleable.

    Penegate I believe told me that after 100 seconds a post will timeout? Is that true???

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  7. #7

    Thread Starter
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Thread.Sleep or Timer in a web service - which is proper??

    Quote Originally Posted by gep13 View Post
    ...For what you have described, I would be more looking to use something like an open WebSocket, rather than any form of polling....
    Scared of new stuff- HTML5 - what if all my clients don't run newer browsers - yadda yadda...

    Why do I always need to do things that push the envelope!!

    Is a TIMER bad practice in a web service?

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  8. #8
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Thread.Sleep or Timer in a web service - which is proper??

    Quote Originally Posted by szlamany View Post
    Scared of new stuff- HTML5 - what if all my clients don't run newer browsers - yadda yadda...

    Why do I always need to do things that push the envelope!!
    Then you pop up an error message telling them to go and get a new browser That is what Trello did:

    https://trello.com/

    Seriously though, it is a valid concern.

    Quote Originally Posted by szlamany View Post
    Is a TIMER bad practice in a web service?
    Is it a bad practice? Tough call. My gut says yes. Web Services, and Web Applications are meant to be stateless. Introducing a Timer, in my opinion, is building in at least some form of state, and should be avoided. If we are talking the difference of waiting an extra minute to get the report as a result of a poll from the client, is that really such a bad thing?

    Could your back end service not email a link, or for that matter the report itself, to the user when it is ready?

    Gary

  9. #9

    Thread Starter
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Thread.Sleep or Timer in a web service - which is proper??

    Let me argue a different point of view...

    If I has a web service that actually created reports - PDF's or whatever output format - would it be acceptable for that web service to take 60 seconds to run. So POST comes in and no response for 60 seconds...

    Or would you expect the web service to off-load the report creation to some other application on the server - so the POST for the report comes in and the response is immediate??

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  10. #10
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Thread.Sleep or Timer in a web service - which is proper??

    Hello,

    That really depends on what you want to do on the UI during this time.

    Do you want the user to interact with your application while the report is being generated, or do you want them to be presented with some form of waiting dialogue while it is happening.

    Depending on the application, both are perfectly valid.

    For what I am working on just now, we make the call to our Web Services asynchronously, allows us to continue work on the UI while waiting for the callback from the method.

    Gary

  11. #11

    Thread Starter
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Thread.Sleep or Timer in a web service - which is proper??

    Same here - all the async calls I do leave the UI free - user can change jQuery TABS and work on other grids - I just make sure to lock-up whatever I need to on the tab that is waiting.

    There are times where the app waits for initial autocomplete data to come in and I make sure to read-only those dropdowns until the data is available.

    My question was specifically about the SERVER thread running the web service.

    Are you comfortable with a web service taking 60 seconds to complete a task? Does that lead to scalability issues?

    The whole point of this conversation is to alert the user as quickly as possible that a task is complete on the server.

    Considering my options - would you do #1 or #2?

    1) Make an ajax request every 250 ms to the server for the report status

    2) Make an ajax request every minute to the server and have the server respond as soon as it knows the task is complete - basically web service loops internally every 250 ms so the response is instant - at one minute it returns status of "still waiting" and client makes another ajax request

    3) Is there any other option?

    Here's a screen shot of the REPORT selection - then clicking the PRINT ICON to start the report - and a screen shot to the right of that showing a report getting QUEUE'd because one is not done and then what it looks like when the status comes back completed.
    Attached Images Attached Images  

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  12. #12
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Thread.Sleep or Timer in a web service - which is proper??

    Hello,

    How many users are we talking about, and how many reports are they doing on a daily basis?

    If you are going to have say 1000 users, doing a report at the same time every day, then doing a AJAX call every 250 ms to the server is going to rack up.

    I see what you are getting at, and what you are trying to achieve, but is this an over complication? Is this an explicit requirement, or a nice to have? For instance, could you not simply have a refresh button that the user could click to check whether the report is ready?

    If you have to do this, and your options are 1 or 2, then I would say number 2.

    Gary

  13. #13

    Thread Starter
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Thread.Sleep or Timer in a web service - which is proper??

    My clients are complaining about having to hit a SAVE button - used to enter data in a VB6 msflexgrid and I would autosave at the end of each row!

    They are going to complain that the report doesn't just download automatically - why do they have to click the PRINTER ICON to start a report and then wait until it says it's ready to download - and "what do you mean I've got to click DOWNLOAD?!!?!??!".

    When you write custom software for clients - and want to get paid the best $$'s - they are always right. Customer is always right

    Initial use will be dozens of users - running several reports a day.

    But I've got clients with 100's and even one with 1000+ users with the old VB6 app - when we migrate them things are going to have to scale.

    At this point I've tested this code - and my CPU on my development machine does not go crazy. I do not want to use a TIMER as that starts on another thread and that seems really dangerous in IIS as it's got it's own threading issues to deal with. I'm going to move the DB CONNECTION OPEN outside the loop - no reason to open/close a connection 20 times in a loop...

    Code:
    If ctrloption = "reportstatus" Then 
        Try 
            Dim maxloop As Integer = 20 
            Dim curloop As Integer = 0 
            Do While strMessage <> "C" And curloop < maxloop 
                Using dcn As New SqlConnection(System.Web.Configuration.WebConfigurationManager.ConnectionStrings("LocalSQLServerAWC").ToString) 
                    Using cmd As New SqlCommand 
                        cmd.CommandType = CommandType.StoredProcedure 
                        cmd.CommandText = "dbo.awc_ReportGetStatus" 
                        cmd.Connection = dcn 
                        cmd.CommandTimeout = 0 
                        DetermineParameters(dcn, cmd) 
                        cmd.Parameters("@RptId").Value = ctrlval1 
                        dcn.Open() 
                        strMessage = cmd.ExecuteScalar().ToString 
                    End Using 
                End Using 
                If strMessage <> "C" Then 
                    curloop += 1 
                    Threading.Thread.Sleep(250) 
                End If 
            Loop 
            .NewObject("reportstatus", "true") 
            .Seperate() 
            .NewObject("rptid", ctrlval1) 
            .Seperate() 
        Catch ex As Exception 
            strSuccess = "" 
            strMessage = ex.Message 
        End Try
    End If

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  14. #14
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Thread.Sleep or Timer in a web service - which is proper??

    Quote Originally Posted by szlamany View Post
    I'm going to move the DB CONNECTION OPEN outside the loop - no reason to open/close a connection 20 times in a loop...
    Agreed.

Posting Permissions

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



Click Here to Expand Forum to Full Width