Hassan
Hassan

Reputation: 45

why thread comes before response.write

when I run the code, the thread process before the response.write !! why and how to make them in order ?

insertUser.ExecuteNonQuery()
con.Close()
Response.Write("Done successfully ...")
Thread.Sleep(4000)
Response.Redirect("Default.aspx")

Upvotes: 2

Views: 1285

Answers (3)

David
David

Reputation: 219037

A response is a one-time thing in a web application. You can't "respond a little, do something else, and respond some more." This is especially true when you consider something like Response.Redirect() which modifies the headers of the response. (Essentially, Response.Redirect() will entirely "clobber" any content that you've added to the response so that the user will never see it.)

It looks like what you're trying to do here is:

  1. Show the user a message.
  2. Wait a few seconds.
  3. Send the user to another page.

There are a couple of standard ways to accomplish this. You can either respond with a page that includes step 1 which, in client-side code, performs steps 2 and 3 or you can perform step 3 in server-side code and on the second page perform step 1 (and possibly two, hiding the message after a few seconds).

For example, let's say you want to show the message on Page A, wait a few seconds, then send the user to Page B. Then in Page A you might include something like this:

<script type="text/javascript">
    $(function() {
        $('#dialog-message').dialog({
            modal: true,
            buttons: {
                Ok: function() {
                    $(this).dialog('close');
                }
            },
            close: function() {
                window.location.href='Default.aspx';
            }
        });
    });
</script>
<div id="dialog-message">Done successfully ...</div> 

Using jQuery, what this does is show the user a dialog (using the jQuery UI Dialog) with the intended message, and when the user closes the dialog it then performs the redirect.

Upvotes: 1

sreejithsdev
sreejithsdev

Reputation: 1210

Web Response will get on the page only after the complete processing of webrequest,ie you can see response after the excution of code completly.So your code is excuting correct order.You can test it by insert Response.End() methord as shown below

insertUser.ExecuteNonQuery()
con.Close()
Response.Write("Done successfully ...")
Response.End();
Thread.Sleep(4000)
Response.Redirect("Default.aspx")

Upvotes: 0

Microsoft DN
Microsoft DN

Reputation: 10030

You can do it using client side functionality in your code plese refer following link

http://social.msdn.microsoft.com/Forums/da/sharepointdevelopmentprevious/thread/087c6b95-fe8d-48ea-85e6-b7fbcb777a5c

Upvotes: 0

Related Questions