C# 异步操作

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; namespace ConsoleApplication22 { /// /// 异步操作 /// ///  ///  /// delegate int MainRun(int data,int sleep); class Program { static void Main(string[] args) { MainRun mainRun = TakeAWhile; IAsyncResult ar = mainRun.BeginInvoke(1, 300, null, null); while (!ar.IsCompleted) { Console.WriteLine("."); Thread.Sleep(100); } int result = mainRun.EndInvoke(ar); Console.WriteLine("Result :{0}",result); } static int TakeAWhile(int data, int sleep) { Console.WriteLine("TakesAWhile stated"); Thread.Sleep(sleep); Console.WriteLine("TakesAWhile completed"); return ++data; } } }

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default4.aspx.cs" Inherits="Default4" %>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>

    <title></title>

    <link href="Styles/Site.css" rel="stylesheet" type="text/css" />

    <script type="text/javascript">

        $(function () {

            //¦¨¨??º?¡Â

            $('#ajax01').ajaxStart(function () { $(this).show() }).ajaxSuccess(function () { $(this).hide() }).ajaxError(function () { $(this).hide(); alert(msg) });

            $('#btnGet').click(function () {

                var number = $('#Text1').val();

 

                $.get('Handler.ashx?number=' + number, function (result) {

                    alert(result);

                });

 

            });

            $('#btnPost').click(function () {

                var number = $('#Text1').val();

                $.post('Handler.ashx', { 'number': number }, function (result) {

                    alert(result);

                });

 

            });

 

        });

   

    </script>

</head>

<body>

    <form id="form1" runat="server">

    <div id="ajax01">

        <img src="image/loading.gif" />Loading...<br>

        ?º?¨?°??ºyÁ?:<input id="Text1" type="text" /><br />

        <input id="btnGet" type="button" value="get Mode" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

        <input id="btnPost" type="button" value="post  Mode" /></div>

    </form>

</body>

</html>

 

////Hander.ashx

 

<%@ WebHandler Language="C#" Class="Handler" %>

 

using System;

using System.Web;

 

public class Handler : IHttpHandler {

   

    public void ProcessRequest (HttpContext context) {

       

        context.Response.ContentType = "text/plain";

 

 

        int number = 0;

        int.TryParse(context.Request.Params["number"], out number);

        context.Response.StatusCode = 200;

        context.Response.Cache.SetCacheability(HttpCacheability.NoCache);

       

       

        context.Response.Write(string.Format("{0}º?{1}",number,Math.Pow(3,number)));

        context.Response.End();

    }

 

    public bool IsReusable {

        get {

            return false;

        }

    }

 

}

 

 

原文地址:https://www.cnblogs.com/fat_li/p/1923295.html