日期:2014-05-18 浏览次数:21235 次
using System;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.Threading;
using System.Runtime.Remoting.Messaging;
public delegate int Comuter(int a, int b);
public delegate void DivCompleteHandler(object sender, DivCompleteEventArgs e);
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class DivideService : System.Web.Services.WebService
{
public event DivCompleteHandler OnDivComplete;
public DivideService()
{
}
[WebMethod]
public int Div(int a, int b)
{
Thread.Sleep(10000);
try
{
return a / b;
}
catch (DivideByZeroException e)
{
throw e;
}
}
[WebMethod]
public void DivAsyn(int a, int b)
{
Comuter computer = new Comuter(this.Div);
computer.BeginInvoke(a, b, new AsyncCallback(Response), null);
}
void Response(IAsyncResult result)
{
try
{
Comuter computer = ((AsyncResult)result).AsyncDelegate as Comuter;
if (null != computer)
{
int consult = computer.EndInvoke(result);
OnDivComplete(this, new DivCompleteEventArgs(consult, null, false, null));
}
}
catch (DivideByZeroException e)
{
OnDivComplete(this, new DivCompleteEventArgs(0, e, true, null));
}
catch (Exception e)
{
OnDivComplete(this, new DivCompleteEventArgs(0, e, true, null));
}
}
}