Async Compute on User interaction - asp.net

I have a standard web page with Ajax update panel. WorkFlow of the page is : The user can add some entity, delete some entity(on click of their respective buttons) to create some definition. And then user can save these definition, edit it and delete it.
Upon adding/deleting the entities, a complex calculation needs to take place which would be time intensive. What I want is, when the user adds or deletes some entity, a separate thread should do the computation and update the UI. The point to be kept in mind here is, when the entity are added or deleted , the corresponding server side code need to be run first, which adds/deletes the entities from a collection stored in session(that means I can not launch a ajax call on the button, because the server side code for the button needs to run first), and then the logic for computation.
What I have thought of doing is
1. Make the server call for button click
2. When response returns, use AJAX client life cycle to catch the response before page is rendered. This is the place where I would make ajax call, if request was for add/delete entity.
This how ever seems over complicated. I am sure, there must be a easy way to do this.

Found out answer to my problem. What I needed was PageAsyncTask which is in built in asp.net for these kind of jobs.
Google

Related

Update a column when closing a window in asp.net

I have a table of records that has a column processed. In my web page i'm using sessions.. because every user that opens the page should see different data from the table.. so i initially have processed=0 and when i'm selecting my data i'm updating the column where processed=0 and then i'm updating the column to processed=2 so that way if another opens the page he gets other data... but the problem is that if the user closes the page without changing anything about the page i need to put my column back to 0 processed=0 but i can't handle an event on the close button of the page... and also not on the log out because they may close the page without logging out... so does anyone has any idea how can i manage this?
note that i'm using asp.net with vb.net
for what you need, don't rely on some browser action, i.e. browser close button (what if internet goes down for one of the users :) ).
The simple way to achieve this is to perform somekind of polling at both levels, i.e. in the db(since your flag is stored in the db) and at the code. Polling implies, constantly making request for certain operation after a fixed interval of time. Obviously, polling is taxing, but it is one of the solution.
Another way to do is to as soon as your user Logs in, you create a Http Long lived request, which neither of the parties(client and server) break and as soon as it ends, you set the flag to 0 again, but having a parallel long lasting request along with all those others is not simple. It is termed as Comet
So I would recommend, to constantly make an ajax request say every 2minutes, to update a certain field, say LastActive in user accounts table. This would constitute your Code side Polling
and Then create a sql server job, which constantly monitors this LastActive field and say, if the difference between it and current DateTime is more than 2.10 minutes, it sets the processed=0 for that user.
You can also look into SessionExpire fields(or whatever it is called), if you are using Forms Authentication of ASP.NET through Session

UITableViewCells and NSURLConnection

I have a custom UITableViewCell, and when the user clicks a button i make a request to a server and update the cell. I do this with a NSUrlConnection and it all works fine (this is all done inside the cell class) and once it returns it fires a delegate method and the tableivew controller handles this. However when i create the cell in the tableview, i use the dequeue method and reuse my cells. So if a cell has fired a asynchronous nsurlconnection, and the cell gets reused whilst this is still going on, will this in turn erase the current connection? I just want to make sure that if the cell is reused, the actual memory that was assigned to the cell is still there so the connection can fulfil its duty??
You can customize the behavior of a UITableViewCell by subclassing it and overriding the -perpareForReuse method. In this case, I would recommend destroying the connection when the cell is dequeued. If the connection should still keep going, you’ll need to remove the reference to it (set it to nil) and handle its delegate methods elsewhere.
It's never a good idea to keep a reference of a connection or any data that you want to display in a cell, no matter how much of effort you put into it afterward to work around to arising problems. Your approach will never work reliable.
In your case, if the user quickly scrolls the table view up and down, your app will start and possibly cancel dozens of connections and yet never finishes to load something. That will be an awful user experience, and may crash the app.
Better you design your app with MVC in mind: the cell is just a means to display your model data, nothing else. It's the View in this architectural design.
For that purpose the Table View Delegate needs to retrieve the Model's properties which shall to be displayed for a certain row and setup the cell. The model encapsulates the network connection. The Controller will take the role to manage and update change notification and process user inputs.
A couple of Apple samples provide much more details about this topic, and there is a nice introduction about MVC, please go figure! ;)
http://developer.apple.com/library/ios/#documentation/general/conceptual/devpedia-cocoacore/MVC.html
The "Your Second iOS App: Storyboards" also has a step by step explanation to create "Data Controller Classes". Quite useful!
Now, when using NSURLConnection which updates your model, it might become a bit more complex. You are dealing with "lazy initializing models". That is, they may provide some "placeholder" data when the controller accesses the property instead of the "real" data when is not yet available. The model however, starts a network request to load it. When it is eventually loaded, the model must somehow notify the Table View Controller. The tricky part here is to not mess with synchronization issues between model and table view. The model's properties must be updated on the main thread, and while this happens, it must be guaranteed that the table view will not access the model's properties. There are a few samples which demonstrate a few techniques to accomplish this.

Preventing timers from running when something is already busy (using Qt)

In my mathematical application I am using timers to regularly perform certain actions. These actions can also be configured by my users. Now I don't want these actions to be executed if there is already another action busy.
E.g. if the user just started a complex calculation by selecting a menu entry, I don't want to execute the actions behind my timers.
Problem is that the user can execute an action via a lot of different ways (via the menu, by clicking somewhere, via popup menu, via drag-and-drop, ...). What I effectively want is to prevent the timers from going off if the application is currently not in the main event loop.
I will give a more concrete example to make it clearer:
At startup I create the timers
If a timer goes off, I execute some actions which, in practice, could access almost every bit in may application's data structure.
Now suppose the user starts a mathematical algorithm (via the menu, by clicking or by dragging elements on the screen, it doesn't matter how he started it).
The algorithm will perform lots of calculations (in the main thread). Since they are executed in the main thread, the timer events will not go off.
Now the algorithm shows a message box (could be a warning or a question).
While the message box is open, events are processed again, including my timer events, which could possibly perform incorrect calculations because there is already another algorithm running.
Reworking my application so that I move logic to a separate worker thread, or adding checks to all of my actions isn't possible at this moment. So please don't suggest to completely rework my application.
What I tried so far is the following:
Using postEvent to send an event, hoping that this event would only be executed in the main event loop. Unfortunately, also the message box's event loop seems to process posted events.
Using the QEvent::WindowBlocked and QEvent::WindowUnblocked events to see when a modal dialog was opened. In my timer-event-logic I can check whether we are between QEvent::WindowBlocked-QEvent::WindowUnblocked calls or not. Unfortunately, these events only work for modal dialogs created by Qt itself, not for other dialogs (e.g. the Windows MessageBox, or the system's printer configuration dialog). Also, this trick would not help if there would be other event loops created by sub routines.
What I actually need to solve my problem is a simple function, that:
If the application is handling an event in the main event loop returns true
If the application is handling an event in another [sub] event loop, it returns false
An alternative could be to return a level that indicates the 'depth' of the handled event.
Anyone suggestions?
You could hook into the event loop of your main thread/application using QAbstractEventDispatcher. Conditionaly filter out QTimer-events based on your application state.

How to update a page from a worker thread

I have a ASPX page which queries from a database. Once we have the dataset it is bound to a gridview and displayed on the page. All this happens in the Page_Load event.
Ofcourse this a simplistic approach. What is the best way to inform the user that data is being retrieved and when we have the data to update the page with the results in the dataset.
I want all this to happen within the same ASPX page and do not want to hop around pages to achieve this. I looked at update panels however it wasn't clear to me how this could be done with an update panel without having a control which triggers the update for the update panel. There are no controls on my page whhich initiate the database query, it occurs as the page is loaded.
If I do the query in a worker thread and then call the Update method on a UpdatePanel with the gridview as part of it, it doesn't work. Nothing happens.
Any thoughts or help? Thanks.
Well, this is a good question. Personally I have two pretty similar methods to do this:
Have a java script that will make an UpdatePanel reload with a short interval. This will create a series of post-backs to the server. During each post-back you should chek you worker thread and return immediately with the state report, usually one of error, pending, success + data
With a java script, make an asynchronous request to a web-service that will block until the data is fetched. This method brings no latency as compared to the previous one (the time between polls), but may suffer from some browsers/servers attitude to hanging open connections. This is normally solved by some interval (say, 1 minute) introduced, so that the hanging request will return with a message like need more time, in which case the java script should simply repeat the request.

ASP.NET Ajax - Asynch request has separate session?

We are writing a search application that saves the search criteria to session state and executes the search inside of an asp.net updatepanel. Sometimes when we execute multiple searches successively the 2nd or 3rd search will sometimes return results from the first set of search criteria.
Example: our first search we do a look up on "John Smith" -> John Smith results are displayed. The second search we do a look up on "Bob Jones" -> John Smith results are displayed.
We save all of the search criteria in session state as I said, and read it from session state inside of the ajax request to format the DB query. When we put break points in VS everything behaves as normal, but without them we get the original search criteria and results.
My guess is because they are saved in session, that the ajax request somehow gets its own session and saves the criteria to that, and then retrieves the criteria from that session every time, but the non-async stuff is able to see when the criteria is modified and saves the changes to state accordingly, but because they are from two different sessions there is a disparity in what is saved and read.
EDIT:::
To elaborate more, there was a suggestion of appending the search criteria to the query string which normally is good practice and I agree thats how it should be but following our requirements I don't see it as being viable. They want it so the user fills out the input controls hits search and there is no page reload, the only thing they see is a progress indicator on the page, and they still have the ability to navigate and use other features on the current page. If I were to add criteria to the query string I would have to do another request causing the whole page to load, which depending on the search criteria can take a really long time. This is why we are using an ajax call to perform the search and why we aren't causing another full page request..... I hope this clarifies the situation.
Just another thought, I've always run into problems with updatepanel and prefer to write my atlas ajax requests through the library directly, using PageMethods. You have more control over what you send and receive. UpdatePanel sends the entire page, and receives the entire page control heirarchy, then it parses out what is 'fresh' and displays that.
Edit: What is the code you're using to save the criteria to the session? And do you have code in the method that actually checks to see if the session has some saved criteria, and passes that back instead? Maybe that's why the 2nd/3rd updatepanel postbacks are returning the first set of criteria instead of the expected results? As an aside, I know from doing some heavy atlas ajax things that there is definitely not two sessions (one for normal postback, one for async) Is there any chance you're using a webfarm?
Edit #2: I wouldn't have been able to write what I've written above (first para) if I hadn't been a fan of someone who replied as well: https://stackoverflow.com/users/60/dave-ward
There are not multiple sessions between normal ASP.NET page loads, postbacks, and ASP.NET AJAX partial postbacks. I can tell you that with certainty.
Rather than storing the search string in the session, how about just using the search TextBox's contents directly? I can't think of any reason why you'd need to shuffle it around, since it will be available throughout the entire page lifecycle anyway.
Finally, concerning your requirements... Using an UpdatePanel does not fulfill the requirement that your users should be able to use other functionality on the page if that functionality also raises partial postbacks. Only one partial postback can be in progress at a time. If another event is raised while your search is in progress, the search request will be canceled without any notification.
Using a page method or web service for the search would be a much faster, easier, and more robust way of doing it. I don't usually plug my own site, but I think a couple of my posts are exactly relevant to what you're doing:
You could use a user control to render the search results through a web service (very much faster than an UpdatePanel): http://encosia.com/2008/02/05/boost-aspnet-performance-with-deferred-content-loading/
Or, you could return the search results as JSON and render that on the client side (even faster): http://encosia.com/2008/06/26/use-jquery-and-aspnet-ajax-to-build-a-client-side-repeater/
Either of those methods could take your search functionality out of the partial postback paradigm, so that it runs faster, uses less bandwidth and server resource, and doesn't preclude other UpdatePanel activity from occurring concurrently.
You need to set the EnableSession property of the WebMethod attribute for the function you are calling.
[WebMethod( EnableSession=true )]
public static void DoSomething(){
/// ....
}
If you use generic handlers .ashx, just derive from IRequiresSessionState interface
public class ActionRequest : IHttpHandler, IRequiresSessionState
{
}

Resources