Threads

  • Not a SQL question but VB.NET so don't kill me but I got some friends here , maybe somebody knows the answer...

    I was given somebody's elses application to work on  and it has a bug that I cannot fix. The guy gets an order from a customer.

    After that in the sub he creates a new thread that calls sub insertOrder() and at the same time he redirects to the confirmation page.

    In the thread he submits the order to the database, gets order number from the database and put it into the session(orderID) since he cannot return order number.

    In the confirmation page he gets that orderID from the session(orderID) and shows it to the customer.

    Problem is that sometimes there is session(orderID) and sometimes there is not. Sometimes it redirect faster than other thread creates that session(orderID).

    So I have to refresh the confirmation page after it was loaded in order to view that orderID.

    I tried so many things to fix it, I already don't know what to do. I have to keep that thread creation since I do not own the code. I tried to set

     

    NewThread.Priority = ThreadPriority.Highest

    so thread would execute faster than redirection happens, but it didn't help in all 100%  cases. I still have every other orderID not showing up. Spent several days on it and got completely desperate since there are plenty of other bug to fix and development to do..  I need some hack. Maybe to reload confirmation page just once when it loads, so it will get that session(orderID).

    How to reload page just once in VB.NET? Or what other possible solution could be? But I cannot get rid of the new thread...

     

    Protected Sub SaveOrder_Thread()

    Dim NewThread As Thread = New Thread(AddressOf  Me.InsertOrder)     

    NewThread.Priority = ThreadPriority.Highest

    NewThread.Start()

    Response.Redirect("confirm_order.aspx")

    End Sub

     

    Protected

    Sub InsertOrder()

    'Insert into DB here and create session("orderID"

    End Sub

     

  • you will have to syncronize or serialize the calls in some way shape or form. http://www.codeproject.com do a search on lock, syncronziation or serialization. I don't do vb at all or I'd give you some sample code.

    Wes

  • The trouble is that you don't know how long the database processing time will be, so my suggestion is that you wait with the response.redirect until the insert order thread has completed. One way to do this would be to use a timer that checks whether the session variable is set. You should also use timeout to avoid waiting indefinitely.

    But, ofcourse, a better solution would be to remove the thread, and run the insert order on the same thread.

    /Pelle

  • Remove the new thread.  You have said that you need to wait for the order insert to complete before displaying it (obviously).  The point of creating a new thread would be to not wait for something to process.  This is simply not what you are trying to do.  Yes, your web page will hang until the order is saved, but I think that is what you want anyway.

    Change the procedure to:

    Protected Sub SaveOrder_Thread()

    Me.InsertOrder()

    Response.Redirect("confirm_order.aspx")

    End Sub

     

    You are way outside of the correct forum for this one.

  • or you could consider using Ajax in your web app.


    Everything you can imagine is real.

  • Thanks everybody for help. Anybody can recommend a good forum for .NET questions? I have to talk to the owner of the code regarding removing the thread...

     

  • Try this forum instead:

    http://forums.asp.net/

Viewing 7 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic. Login to reply