Asp.Net非同期呼び出しWebService

2254 ワード

//    
[WebMethod]
public string Test(int sleepTimes, int val)
{
    Thread.Sleep(sleepTimes);
    var log = AppDomain.CurrentDomain.BaseDirectory + "/logs/log.log";
    using (var sw = new StreamWriter(log, true))
    {
        sw.WriteLine(DateTime.Now.ToString() + " -> " + val.ToString());
    }
    return DateTime.Now.ToString() + " -> " + val.ToString();
}
//asp.net    
protected void btnInvoke_Click(object sender, EventArgs e)
{
    ThreadPool.QueueUserWorkItem((obj) =>
    {

        for (var i = 1; i < 300; i++)
        {
            var demo = new Demo();
            demo.TestCompleted += (s, ex) =>
            {
                if (ex.Error != null)
                {
                    DbLoger.Log(ex.UserState.ToString().ToInt(), ex.Error.Message);
                    demo.TestSyncAsync(1000, ex.UserState.ToString().ToInt());
                } else
                {
                    //DbLoger.Log(ex.UserState.ToString().ToInt(), "");
                }
               
            };
            demo.TestSyncAsync(1000, i, i);
        }
    }, "test");
    Response.Write("DONE");
}