I have to send three asynch requests in three class files, 3 requests response times are different, first one is 2sec and second one is 7sec and third one is 4sec , now i have to display first response in browser with in 2sec and after 2sec display the third response and finally display the second response , but now my results all responses display at a time after completed three responses, please give me any suggestion, it is very urgent, please…..
my code is
public delegate string AsyncMethodCaller(string name);
public delegate string AsyncMethodCallertest(string name);
public delegate string NatilusAsyn(string name);
button click event
AsyncMethodCaller caller = new AsyncMethodCaller(ps.PennStarService);
IAsyncResult result = caller.BeginInvoke(txtFirst.Text, null, null);
NatilusAsyn caller123 = new NatilusAsyn(cs.PennStarService);
IAsyncResult result123 = caller123 .BeginInvoke(txtthird.Text, null, null);
AsyncMethodCallertest cltest = new AsyncMethodCallertest(ps.testHi);
IAsyncResult tetsresult = cltest.BeginInvoke(txtSecond.Text, null, null);
lblFirst.Text = caller.EndInvoke(result);
lblSecond.Text = cltest.EndInvoke(tetsresult);
lblthird.Text = caller123.EndInvoke(result123);
thank u
hemanth
I think the root of the problem is to understand what happens with an ASP.Net request.
Each page has its life-cycle which includes a pipeline of events, for more information check this answer. Each request is handled by a worker thread from the AppDomain of the current application. The response won’t be sent to the user until the page pipeline is completed.
About threads:
The number of simultaneous available threads can be configured in the
machine.config. The key point to understand is that these number of threads are fixed, which means that when the max number of simultaneous threads has been reached, subsequent requests will be placed in a queue, the number of requests that can be placed in the queue are only limited by the memory of the server.When a worker thread calls a long time-consuming operation, you are blocking that thread, until the operation finishes, which means again, if there are several concurrent users, potentially all available threads could be blocked, forcing new requests to be placed in a queue and in the worst case, causing a 503 error – Service Unavailable.
The way to prevent this is by calling these kind of methods in background threads. Similar to your code, but the behavior is not what you are expecting in this case, your code is waiting for the threads to end in order to finish the page request that’s the reason you are receiving the result at the same time (at the end of the request).
For more details about executing asp.net pages asynchronously, refer to this excellent article
Now in order to get the results you need:
(This is a full working example, in this example, I am using Rx – Reactive Programming to run long time-consuming methods in a new thread, you can change this to use another framework if you like, I prefer using Rx because I feel more comfortable with the API)
Using PageMethods
Code behind
ASPX
Output
This code generates the following:
As you can see the code is not optimized, the main thread is being locked, the max number of seconds we set up are 7 but in this case, from the time the server code was called (02:11:17) until the last response received (2:11:30) 13 seconds elapsed. This is because ASP.Net by default locks the current
Sessionobject locking the main thread. Since we are not using the session in this example, we can configure the page like this:And the new output is:
Now, only 7 seconds elapsed from the first server method call to the last response received without locking the main thread =)
Edit 1
If my understanding is correct, you need to pass some parameters to the methods and return datasets to fill labels and textboxes.
To pass parameters to the PageMethods, this is a way to do it:
Install these Nuget Package:
Command class. This class will represent the parameters you want to send to the server method in the post action
Instead of returning a
DataSet, I think it would be easier and more manageable to return anIEnumerable, something like this:The
MyResultclass represents the data you want to send back to the userIn your ASPX page
Edit 2
This is a simplified way
Code behind
Output