Preventing Race Conditions

Posted by Qua on Stack Overflow See other posts from Stack Overflow or by Qua
Published on 2010-12-21T19:54:20Z Indexed on 2010/12/21 22:54 UTC
Read the original article Hit count: 241

I'm using the built-in ajax functionality of MVC2. Basically the user types a search query, and on every key press a set of results for the query is shown. This works fine in almost all cases, but sometimes the server is a bit slow with a single response, and thus the result for the next key stroke is returned before the previous. When the previous key stroke result set is finally returned to the client it will overwrite the results for the newer search query that should actually have been shown.

My code follows more or less along these lines:

    <% using (Ajax.BeginForm("SearchUser", null, 
             new AjaxOptions() 
                    { UpdateTargetId = "findUserResults" }, 
             new { id = "findUserAjaxForm" })) {%>

Every keystroke submits this form and thus outputs the results in the 'findUserResults' element.

How can I prevent older results from being displayed while still making use of the built-in functions provided in MVC2?

© Stack Overflow or respective owner

Related posts about .NET

Related posts about AJAX