I see a couple of problems in the code.
You have a busy loop in the main thread: while(bEnd == false); This probably
does not prevent the asynchronous call from completing because the thread
pool threads run by default with the same priority as your main thread.
Nevertheless a busy loop like this is considered bad. Add a call to
Thread.Sleep to the loop.
A possible problem that could make your program hang is that there is an
exception when you call the EndPrime method. Since you don't handle the
exception and it occurs in the threadpool thread, it just disappears
silently. To find out what the problem is, add a try/catch to your
asynchronous callback:
public static void PrimeCallback(IAsyncResult arResult)
{
try
{
// Obtains the last parameter of the delegate call.
Console.WriteLine ("Inside PrimeCallback");
// get the "this" pointer that was passed...
WSPrime wsp=(WSPrime)arResult.AsyncState;
// call "EndPrime" and get the result..
int iCount=wsp.EndPrime(arResult);
/// show the result of the webservice call..
Console.WriteLine("Async. WebService call found {0} primes.", iCount);
// set flag so that application can terminate...
bEnd=true;
}
catch (Exception ex)
{
Console.WriteLine("Web Service call failed: " + ex.ToString());
bEnd = true;
}
}
If there is an exception, at least this helps you see what the problem is.
Sami
nirk said:
Hi Jan
The web service code is one of yur articles in the code project.Client i created in VS.net envoirment.
CLIENT CODE
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
private static bool bEnd= false;
public static void PrimeCallback(IAsyncResult arResult)
{
// Obtains the last parameter of the delegate call.
Console.WriteLine ("Inside PrimeCallback");
// get the "this" pointer that was passed...
WSPrime wsp=(WSPrime)arResult.AsyncState;
// call "EndPrime" and get the result..
int iCount=wsp.EndPrime(arResult);
/// show the result of the webservice call..
Console.WriteLine("Async. WebService call found {0} primes.", iCount);
// set flag so that application can terminate...
bEnd=true;
}
[STAThread]
static void Main(string[] args)
{
try
{
int intTemp;
IAsyncResult ar;
Console.WriteLine("Calling WebService Asynchronously...\n");
AsyncCallback ascb = new AsyncCallback (Class1.PrimeCallback);
//for (int i =2;i<=10;i++)
{
WSPrime wsp = new WSPrime();
//intTemp = wsp.Prime (10);
wsp.BeginPrime(10,ascb,wsp);
}
Console.WriteLine ("waiting for WebService");
while(bEnd == false);
Console.WriteLine ("Over" );
Console.ReadLine ();
}
//
// TODO: Add code to start application here
//
catch(System.Exception exception)
{
Console.WriteLine (exception.Message);
}
}
};
WEBSERVICE CODE
using System.Web.Services;
public class WSPrime : WebService
{
// This method returns the number of prime number lying between
// 2 and num.
[WebMethod]
public int Prime(int num)
{
int iCount=0;
for(int i=2;i<num;i++)
{
bool bPrime=true;
for(int j=2;j<i;j++)
{
// is this number prime ?
if (i%j==0)
{
// nope.. it isn't...
bPrime=false;
break;
}
}
if (bPrime==true)
iCount++;
}
// return the count..
return iCount;
}
}