we saw how we can communicate with web services using callbacks. Today we will discuss the other two techniques Polling and Waithandles.
Polling :
When we made a call to our web service using callback we passed it reference to our fucntion and in return we get an IAsyncResult object back which acted as a token.
In case of polling we will again call BeginCoSine method but not pass any parameter for callback. This method will return us an IAsyncResult object. Polling works on one of the properties of this object called IsCompleted. After calling the method we will have to continuous POLL for the value of this property. When the webservice is completed with its processing the value IAsyncResult object will be set to true.After that we call the EndCoSine function passing it our IAsyncResult object (which acts as a token) to retrieve our result.
Add another button to your windows form and put in the below code :
private void button3_Click(object sender, System.EventArgs e)
{
int noOfSec = 0;
oIAsyncResult = trigoService.BeginCoSine(90,null,null);
while(oIAsyncResult.IsCompleted == false)
{
noOfSec++;
Thread.Sleep(1000);
}
MessageBox.Show("Completed in " + noOfSec);
label1.Text = Convert.ToString(trigoService.EndCoSine(oIAsyncResult));
}
Here we are checking out the value of IsCompleted property in a while loop. After the value is set to true we message the number of seconds that were taken to POLL. Finally we call EndCosine function passing our IAsyncResult object.
WaitHandles :
This method also uses one of the properties of IAsyncResult object called AsyncWaitHandle.
This approach is generally used when u dont want to leave the thread in which you are communicating with the webservice. Thus in this case you will wait for the webservice to complete its work without releasing the thread in which you were communicating. You can achieve this by using the WaitHandle returned by AsyncWaitHandle property of IAsyncResult object.
After you have called the BeginCosine method and got your IAsyncResult Token object you can use its AsyncWaitHandle property to get WaitHandle object then use its WaitOne method to wait for the thread. WaitOne has multiple overloads some also take the amount of time you want to wait.
Add another button to your windows form and add the following code snippet
private void button4_Click(object sender, System.EventArgs e)
{
int noOfSec = 0;
oIAsyncResult = trigoService.BeginCoSine(90,null,null);
oIAsyncResult.AsyncWaitHandle.WaitOne();
while(noOfSec++ < 5)
{
noOfSec++;
Thread.Sleep(1000);
}
MessageBox.Show("Waited for " + noOfSec);
label1.Text = Convert.ToString(trigoService.EndCoSine(oIAsyncResult));
}
You can also checkout the other static methods of the WaitHandle object like WaitAll and WaitAny. These methods are particularly used when you have multiple web service calls and you want to wait for either ALL of them to complete their processing and then move ahead or with ANY ONE of them to complete their processing and then move ahead.
WAIT ALL :- Wait for all the web services to complete processing.
WAIT ANY:- Wait for any one of them to complete processing.
No comments:
Post a Comment