IAsyncResult是接口:

  IAsyncResult 异步设计模式通过名为 BeginOperationName 和 EndOperationName 的两个方法来实现原同步方法的异步调用,如 FileStream 类提供了 BeginRead 和 EndRead 方法来从文件异步读取字节,它们是 Read 方法的异步版本

Begin 方法包含同步方法签名中的任何参数,此外还包含另外两个参数:一个AsyncCallback 委托和一个用户定义的状态对象。委托用来调用回调方法,状态对象是用来向回调方法传递状态信息。该方法返回一个实现 IAsyncResult 接口的对象

End 方法用于结束异步操作并返回结果,因此包含同步方法签名中的 ref 和 out 参数,返回值类型也与同步方法相同。该方法还包括一个 IAsyncResult 参数,用于获取异步操作是否完成的信息,当然在使用时就必须传入对应的 Begin 方法返回的对象实例

开始异步操作后如果要阻止应用程序,可以直接调用 End 方法,这会阻止应用程序直到异步操作完成后再继续执行。也可以使用 IAsyncResult 的 AsyncWaitHandle 属性,调用其中的WaitOne等方法来阻塞线程。这两种方法的区别不大,只是前者必须一直等待而后者可以设置等待超时

如果不阻止应用程序,则可以通过轮循 IAsyncResult 的 IsCompleted 状态来判断操作是否完成,或使用 AsyncCallback 委托来结束异步操作。AsyncCallback 委托包含一个 IAsyncResult 的签名,回调方法内部再调用 End 方法来获取操作执行结果

//C#异步编程模式IAsyncResult之IAsyncResult 接口
public interface IAsyncResult {
object AsyncState { get; }
WaitHandle AsyncWaitHandle { get; }
bool CompletedSynchronously { get; }
bool IsCompleted { get; }
}

以上是IAsyncResult 接口的属性。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; using System.Threading;
using System.Runtime.Remoting.Messaging; namespace ConsoleTest
{
public delegate int AddHandler(int a, int b);
public class 加法类
{
public static int Add(int a, int b)
{
Console.WriteLine("开始计算:" + a + "+" + b);
Thread.Sleep(); //模拟该方法运行三秒
Console.WriteLine("计算完成!");
return a + b;
}
} #region
public class 同步调用
{
static void Main()
{
Console.WriteLine("===== 同步调用 SyncInvokeTest =====");
AddHandler handler = new AddHandler(加法类.Add);
int result = handler.Invoke(, ); Console.WriteLine("继续做别的事情。。。"); Console.WriteLine(result);
Console.ReadKey();
}
}
#endregion
}

/*运行结果:
         ===== 同步调用 SyncInvokeTest =====
         开始计算:1+2
         计算完成!
         继续做别的事情。。。
         3

*/

public delegate int AddHandler(int a, int b);
public class 加法类
{
public static int Add(int a, int b)
{
Console.WriteLine("开始计算:" + a + "+" + b);
Thread.Sleep(3000); //模拟该方法运行三秒
Console.WriteLine("计算完成!");
return a + b;
}
} #region 异步调用
public class 异步调用
{
static void Main()
{
Console.WriteLine("===== 异步调用 AsyncInvokeTest =====");
AddHandler handler = new AddHandler(加法类.Add); //IAsyncResult: 异步操作接口(interface)
//BeginInvoke: 委托(delegate)的一个异步方法的开始
IAsyncResult result = handler.BeginInvoke(1, 2, null, null); //如果在2秒内完成计算
if (result.AsyncWaitHandle.WaitOne(2000, true))
{
Console.WriteLine(handler.EndInvoke(result));
Console.WriteLine(result);
} Console.WriteLine("继续做别的事情。。。"); //异步操作返回
Console.WriteLine(handler.EndInvoke(result));
Console.ReadKey();
}

运行结果:

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAUEAAACSCAIAAABUse9TAAANtElEQVR4nO2c329cRxXH/efcZz/aj1YeMC4QpyIkyEbiiVYGiZdaQk5QQbRJTLr0l5oEQkhSNz9oU8UUJY2beOOIB5pWqktdKBaobqVWIv4RO6Gx6QsP692dO3POmTN3793dufv9aBTtzp177vz63Jl7Hbvv4Nw0EhJSvKmv4zVAQkJqJcFhJKS4ExxGQoo7wWEkpLiT5PCTF6d/fOaoJj1x4diBG8c63hgkpB5MksN/fHP2vQ+W3vvgIyctvb+49P7i0uzbC7PXb89ev/3a5Sv73niajjMz1p8kSZL0T04NjyfJnrHRuemDc9PmZyQ+TQwkg0MzZH4yUAkKNTW0J0nGJzrdom5JRczANs7q5mhKDs/NL2zv7Dza9qTtnZ25W7f3XjxEBZkYSJL+yanmVfNweHRyMElGhgNzskVuf0rXgXB4eDxJeIH5JvSWw76hTM3GvFLXOTy/cOcf//z8yOlbR8/cPH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Efh0OckYoaR1tv8NkBD36COQVg9wuujwoA97fH67h1dUt73Wgyi/X3CXkwkIlOaPkJugjuEeFHKVmRZcHJcH7XlowzUVYrLg57Roulxeq0aLDwn3HbZQVQVMeDoNCkJ+HG8VkhzWz3D3R67YQylsgyEASjQmCNtwVgwQrujwoA0HPw8Jqo5REvhcIBawLeW8HgvDcPUJfgKtV6OkA5IDyeTgUzdKnWbsEY73nytVrsYBVTFNDAAoh1GHv8qUvn6EYdyKcAb1L6N/ikR0m99hkflBkgdDNPABlI/TveHBWCE+GXBwlcv3lAnAYlJ9cHM6gSoY9dgbn4TAoP/k+DyvtyiAwdxbWYdDrFP1OSz6qXE7lCNlqC0BJ0PwNAP3qqiyZDa/DbgE4DMpPXu+05EPeoxrIOwIcBr1Ovg7r12FvYfIU9zMcBr1ON6zDsofeYlX+v3nAYVB+OrUOczHzdRiA8pOjw7mckk11cmsNQE+gcdjrmHcFFlZjzkbuEFklzmEoDcqP8m8AmDlywNB1WOmw9zO5o4bDoPzku5fmVml9eY23XCa5zgNQctrw/7SE7TdX3tp4628NcBj0HAX9DQAAQJuAwwDEDRwGIG7gMABxwzks/EQXANBFCL97WCsAjQHoarx7aTgMQFcDhwGIG8FhPA8DEAFYhwGIGzgMQNzgvTQAcYOfDwMQN/h/WgDEDRwGIG7gMABxA4cBiBs4DEDcwGEA4gYOAxA3cBiAuIHDAMQNHAYgbuAwAHEDhwGIGzgMQNzAYQDiBg4DEDdwGIC4gcMAxA0cBiBu4DAAcQOHAYgbOAxA3MBhAOIGDgMQN3AYgLiBwwDEDRwGIG7gMABxA4cBiBs4DEDcwGEA4gYOAxA3cBiAuIHDAMQNHAYgbuAwAHEDhwGIGzgMQNzAYQDiBg4DEDdwGICo+T/zq7NmdTPrKAAAAABJRU5ErkJggg==" alt="" />

异步回掉:

 public delegate int AddHandler(int a, int b);
public class 加法类
{
public static int Add(int a, int b)
{
Console.WriteLine("开始计算:" + a + "+" + b);
Thread.Sleep(); //模拟该方法运行三秒
Console.WriteLine("计算完成!");
return a + b;
}
} #region 异步回掉函数
public class 异步回调
{
static void Main()
{
Console.WriteLine("===== 异步回调 AsyncInvokeTest =====");
AddHandler handler = new AddHandler(加法类.Add); //异步操作接口(注意BeginInvoke方法的不同!)
IAsyncResult result = handler.BeginInvoke(,,new AsyncCallback(回调函数),"AsycState:OK");
//IAsyncResult result1 = handler.BeginInvoke(3, 4, new AsyncCallback(CallBackFuncation), "异步回掉完成"); Console.WriteLine("继续做别的事情。。。");
Console.ReadKey();
} static void 回调函数(IAsyncResult result)
{
//result 是“加法类.Add()方法”的返回值
//AsyncResult 是IAsyncResult接口的一个实现类,引用空间:System.Runtime.Remoting.Messaging
//AsyncDelegate 属性可以强制转换为用户定义的委托的实际类。
AddHandler handler = (AddHandler)((AsyncResult)result).AsyncDelegate;
Console.WriteLine(handler.EndInvoke(result));
Console.WriteLine(result.AsyncState);
}

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAVwAAACrCAIAAACllLNuAAAPbElEQVR4nO2c229dRxWH/efs5zzaj1YeMC6QpKKkyEbiiVYGiZdaQm5RQfQS05re1AuE0rRu09CLEop6cZOcJOKBXqS61IUSgepWaiXiS+KEJqEvPJz4eM9lrVmzzz722cffp1Fkz55Zs+ayfntmznGGbl2YldLtR2d//PT9lnTbC4cOvnVIMUUikZqShpRnf3z1xHsfLL33wUdBWnp/cen9xaUTb5498caZE2+cef7YKwdevjtuZ35iT1EURbFnemZssij2TuxfmL11Ybb8M0lOU8PFyOh8NL8YnssyNTO6tygmp3a6R/2SerECt3FVO7NZb7uaKCycPnvt+vWr1xLp2vXrC6fO7Dt6Z8zI1HBR7Jme2epGHaKwf3qkKMbHMnOqWd7+5PoQEYWxyaKQFUHuwu4ShdRUOquxrrRDolC5LzOje4sOm3GqisLps+f+8c/P7zt86v6nTx565u3Z5xYeeOGtjz/+99rqymfLy58tL6+urjx1/N2njr8risL8xJ74i66b4QsXtyWnmuXtT54PZVG4MYXqBkHpQj/0rpuU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VJoz2JcLyXg6iALuCer/RSNgANJ5aRKHCaxkA+hT+9gEAHBAFAHBAFADAAVEAAAdEAQAcEAUAcEAUAMABUQAAB0QBABwQBQBwQBQAwAFRAAAHRAEAHBAFAHBAFADAAVEAAAdEAQAcEAUAcEAUAMABUQAAB0QBABwQBQBwQBQAwAFRAAAHRAEAHBAFAHBAFADAAVEAAAdEAQAcEAUAcEAUAMABUQAAB0QBABwQBQBwQBQAwAFRAAAHRAEAHBAFAHBAFADAAVEAAAdEAQAcEAUAcEAUAMABUQAAB0QBABwQBQBwQBQAwAFRAAAHRAEAHBAFAHBAFADAAVEAgDL/B175S11ql5RHAAAAAElFTkSuQmCC" alt="" />

使用事件异步计算:

 public class TestFun
{
//模拟一个耗时计算
static int LongTimeMethod()
{
int result = ;
//Thread. Sleep(3000);
for (int i = ; i <= ; i++)
{
result += i;
}
return result;
} static void Main()
{ //这里用到的是.Net中定义好的委托来执行BeginInvoke
Func<int> longTimeAction = new Func<int>(LongTimeMethod);
IAsyncResult asynResult = longTimeAction.BeginInvoke(null, null); //WaitOne
//这个对象有一个WaitOne方法,还能接受一个超时时间,它会等待这个超时时间指定的长度
Func<int> longTimeAction1 = new Func<int>(LongTimeMethod);
IAsyncResult asynResult1 = longTimeAction1.BeginInvoke(null, null);
//可以继续处理别的事情
if (asynResult1.AsyncWaitHandle.WaitOne(, true))//判断是不是结果为true,只等你10s
{
int result1 = longTimeAction1.EndInvoke(asynResult1);
Console.WriteLine(result1);
}
  }

运行结果:

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAPUAAABWCAIAAACYW0dyAAAG1ElEQVR4nO2ayW9TRxzH8+fMOUfnaOVQN7RNgkqTyq7UU0FppV6IVBkqWpUlLrhsItCmlIAhpCxKSsVisquHskiYYloatcJUAqnxkjgpScqlB9vP8+bN9mwckl+/H/0OeN7M+M1vPp4380JTdzKGQFCNpld+BwhE4wJ+IygH/EZQDviNoBzwG0E54DeCcpj93jYU+/DkPpvYeq6v60bfK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时计算
static int LongTimeMethod()
{
int result = 0;
//Thread. Sleep(3000);
for (int i = 0; i <= 100; i++)
{
result += i;
}
return result;
} static void Main()
{
//这里用到的是.Net中定义好的委托来执行BeginInvoke
Func<int> longTimeAction = new Func<int>(LongTimeMethod);
IAsyncResult asynResult = longTimeAction.BeginInvoke(null, null); //轮询
while (!asynResult.IsCompleted)
{
//当不是true时,就执行这里的代码
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("计算还未完成,等待中....");
Console.ForegroundColor = ConsoleColor.Yellow;
for (int i = 1; i < 100; i++)
{
for (int j = i; j < 100; j++)
{
int a = i, b = j;
Console.WriteLine("{0}*{1}={2}", a, b, a * b);
}
}
Console.Clear();
}
int result1 = longTimeAction.EndInvoke(asynResult);//当是true时,就将结果返回显示
Console.WriteLine("计算的结果为:" + result1);
Console.ReadLine();
}

运行结果:

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAQMAAABoCAIAAAB69wKxAAAIl0lEQVR4nO2a208UVxzHf3/OPPMIj4SHUmwLmFq2WZr0qTW0SV8kadDGNvXCVrfe4qWlVnQVqZdAbVQQEEgf6iVxrdha0sa1iSZld7lWoD7Qh8Punjlzzm/OzO7Cot9Pfg/umZkz5/Y5l0GKDMYQCAStewkQiEoImIBAxCIwAYEQwZmwtSf20cm9NvHhuc6Wgc51rwwCETo4E3643H/n3sSdew88MXE3OXE3OdF/faz/2mj/tdGzvZeaL+7U55OIVjmO4zhV7R31rY5TF20ajEUGY/K/EeZoq3ZqahPadKc6Hiirjto6x2ltW+8aVUooI5AzYXBkbGl5eXHJJ5aWlweHRxt7ths60qlq7yj0RClMaGqvcZyG+oAp4XJe+3CXQWNCfavjmDUwV+HVMsGvK12jMcKbMDI2/vsff+/pGt57cqjz1I3YmcF95wYePvwrm0k/SaWepFKZTPpE3+0TfbeNJiSiVfoprRgTvD1qkxIu57UPpQyyCR21dZwDflWohNoVE4GWwcCVZU0YHX80+fSD/f0fHuxrO3bp464Ln3T3JpOT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AiGACAAKYAAARTABAABMAIIIJAAhgAgBEMAEAAUwAgAgmACCACQAQwQQABDABACKYAIAAJgBABBMAEMAEAIhgAgACmAAAEUwAQAATACCCCQAIYAIARDABAAFMAIAIJgAggAkAEMEEAAQwAQAimACAACYAQAQTABDABACIYAIAApgAABFMAEAAEwAgggkACGACAET0PysZIPaUI4c8AAAAAElFTkSuQmCC" alt="" />

完整代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Runtime.Remoting.Messaging; namespace ConsoleTest
{
public delegate int AddHandler(int a, int b);
public class 加法类
{
public static int Add(int a, int b)
{
Console.WriteLine("开始计算:" + a + "+" + b);
Thread.Sleep(); //模拟该方法运行三秒
Console.WriteLine("计算完成!");
return a + b;
}
} public class TestFun
{
//模拟一个耗时计算
static int LongTimeMethod()
{
int result = ;
//Thread. Sleep(3000);
for (int i = ; i <= ; i++)
{
result += i;
}
return result;
} static void Main()
{ //这里用到的是.Net中定义好的委托来执行BeginInvoke
Func<int> longTimeAction = new Func<int>(LongTimeMethod);
IAsyncResult asynResult = longTimeAction.BeginInvoke(null, null); ////WaitOne
////这个对象有一个WaitOne方法,还能接受一个超时时间,它会等待这个超时时间指定的长度
//Func<int> longTimeAction1 = new Func<int>(LongTimeMethod);
//IAsyncResult asynResult1 = longTimeAction1.BeginInvoke(null, null);
////可以继续处理别的事情
//if (asynResult1.AsyncWaitHandle.WaitOne(10000, true))//判断是不是结果为true,只等你10s
//{
// int result1 = longTimeAction1.EndInvoke(asynResult1);
// Console.WriteLine(result1);
//} //回调
Func<int> longTimeAction2 = new Func<int>(LongTimeMethod);
//这里使用了一个lambda表达式,省了不少力啊
IAsyncResult asynResult2 = longTimeAction.BeginInvoke((result) =>
{
int ret = longTimeAction.EndInvoke(result);
Console.WriteLine(ret);
}, null); //轮询
while (!asynResult.IsCompleted)
{
//当不是true时,就执行这里的代码
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("计算还未完成,等待中....");
Console.ForegroundColor = ConsoleColor.Yellow;
for (int i = ; i < ; i++)
{
for (int j = i; j < ; j++)
{
int a = i, b = j;
Console.WriteLine("{0}*{1}={2}", a, b, a * b);
}
}
Console.Clear();
}
int result1 = longTimeAction.EndInvoke(asynResult);//当是true时,就将结果返回显示
Console.WriteLine("计算的结果为:" + result1);
Console.ReadLine();
}
} #region 直接调用
//public class 同步调用
//{
// static void Main()
// {
// Console.WriteLine("===== 同步调用 SyncInvokeTest =====");
// AddHandler handler = new AddHandler(加法类.Add);
// int result = handler.Invoke(1, 2); // Console.WriteLine("继续做别的事情。。。"); // Console.WriteLine(result);
// Console.ReadKey();
// }
// /*运行结果:
// ===== 同步调用 SyncInvokeTest =====
// 开始计算:1+2
// 计算完成!
// 继续做别的事情。。。
// 3 */
//}
#endregion
#region 异步调用
//public class 异步调用
//{
// static void Main()
// {
// Console.WriteLine("===== 异步调用 AsyncInvokeTest =====");
// AddHandler handler = new AddHandler(加法类.Add); // //IAsyncResult: 异步操作接口(interface)
// //BeginInvoke: 委托(delegate)的一个异步方法的开始
// IAsyncResult result = handler.BeginInvoke(1, 2, null, null); // //如果在2秒内完成计算
// if (result.AsyncWaitHandle.WaitOne(2000, true))
// {
// Console.WriteLine(handler.EndInvoke(result));
// Console.WriteLine(result);
// } // Console.WriteLine("继续做别的事情。。。"); // //异步操作返回
// Console.WriteLine(handler.EndInvoke(result));
// Console.ReadKey();
// }
// /*运行结果:
// ===== 异步调用 AsyncInvokeTest =====
// 继续做别的事情。。。
// 开始计算:1+2
// 计算完成!
// 3 */
//}
//#endregion //public class 异步回调
//{
// static void Main()
// {
// Console.WriteLine("===== 异步回调 AsyncInvokeTest =====");
// AddHandler handler = new AddHandler(加法类.Add); // //异步操作接口(注意BeginInvoke方法的不同!)
// IAsyncResult result = handler.BeginInvoke(1, 2, new AsyncCallback(回调函数), "AsycState:OK");
// //IAsyncResult result1 = handler.BeginInvoke(3, 4, new AsyncCallback(CallBackFuncation), "异步回掉完成"); // Console.WriteLine("继续做别的事情。。。");
// Console.ReadKey();
// } // static void 回调函数(IAsyncResult result)
// {
// //result 是“加法类.Add()方法”的返回值
// //AsyncResult 是IAsyncResult接口的一个实现类,引用空间:System.Runtime.Remoting.Messaging
// //AsyncDelegate 属性可以强制转换为用户定义的委托的实际类。
// AddHandler handler = (AddHandler)((AsyncResult)result).AsyncDelegate;
// Console.WriteLine(handler.EndInvoke(result));
// Console.WriteLine(result.AsyncState);
// }
// /*运行结果:
// ===== 异步回调 AsyncInvokeTest =====
// 开始计算:1+2
// 继续做别的事情。。。
// 计算完成!
// 3
// AsycState:OK
// */ // static void CallBackFuncation(IAsyncResult result)
// {
// Console.WriteLine("这里是回掉函数");
// AddHandler handler = (AddHandler)((AsyncResult)result).AsyncDelegate;
// Console.WriteLine(handler.EndInvoke(result));
// Console.WriteLine(result.AsyncState); // }
//}
#endregion
}

C# 异步和委托学习的更多相关文章

  1. 异步使用委托delegate --- BeginInvoke和EndInvoke方法

    当我们定义一个委托的时候,一般语言运行时会自动帮委托定义BeginInvoke 和 EndInvoke两个方法,这两个方法的作用是可以异步调用委托. 方法BeginInvoke有两个参数: Async ...

  2. C#异步编程研究学习(一)

    可以使用Func<T>或者Action<T>简单实现如: Func<string, string,string,string, int> func = new Fu ...

  3. 委托学习笔记后续:泛型委托及委托中所涉及到匿名方法、Lambda表达式

    引言: 最初学习c#时,感觉委托.事件这块很难,其中在学习的过程中还写了一篇学习笔记:委托.事件学习笔记.今天重新温故委托.事件,并且把最近学习到和委托相关的匿名方法.Lambda表达式及泛型委托记录 ...

  4. C#委托学习

    标签(空格分隔): C# 看Markdown效果支持的不大好. 买来<CLR Via C#>这本书很久了,一直也没有对其进行总结,看的非常凌乱,趁此机会好好总结一下,也算对C#学习的一个总 ...

  5. 委托学习总结(一)浅谈对C#委托理解

    初入社会,对于我这个初级程序员来说要学的东西实在太多了,公司最近在做一个winform框架开发的桌面应用程序,众所周知,winform也好,webform也好,里面随处可见的事件驱动,有事件,当然也少 ...

  6. .Net进阶系列(11)-异步多线程(委托BeginInvoke)(被替换)

    一. BeginInvoke最后两个参数的含义 倒数第二个参数:指该线程执行完毕后的回调函数:倒数第一个参数:可以向回调函数中传递参数. 下面以一段代码说明: /// <summary> ...

  7. 委托学习总结(二)匿名方法和lambda表达式

    之前总结了委托这个困惑着大多初学者的概念,继续来学习匿名方法和lambda表达式 (1)我们之前写了这样一段代码 //自定义一个委托 public delegate int Expression(in ...

  8. 《C#高级编程》之委托学习笔记 (转载)

    全文摘自 http://www.cnblogs.com/xun126/archive/2010/12/30/1921551.html 写得不错,特意备份!并改正其中的错误代码..     正文: 最近 ...

  9. C# 委托学习笔记

    接触委托 代理 delegate很久啦.除了看API,Kotoba也给我讲了 .说到委托,拿下面这个小例子比较好.(14年6月26花花给我的练习) 实例:写一个方法A,定义个方法B(打印hello), ...

随机推荐

  1. rhel6.4 using centos source

    rhel 默认的 yum 依赖于 rhn,要注册收费才能 update,改用 centos 的源: rpm -e yum* --nodeps rpm -qa | grep yum | xargs rp ...

  2. java对象Integer不能引用传递

    java对象Integer不能引用传递 /** * The value of the <code>Integer</code>. * * @serial */ private ...

  3. Sass函数--字符串函数

    Sass的函数简介在 Sass 中除了可以定义变量,具有 @extend.%placeholder 和 mixins 等特性之外,还自备了一系列的函数功能.其主要包括: ● 字符串函数 ● 数字函数 ...

  4. ASP.NET MVC Controller接收ajax post方式发送过来的json对象或数组数据

    本例旨在说明我的一种Controller接收ajax提交(POST)过来的json对象或数组信息的方式,感觉应该有更好的方式,欢迎提出宝贵意见. JSON.stringify(jsonObj)不支持I ...

  5. (转)ASP.NET 伪静态

    1.伪静态:http://www.cnblogs.com/Default.html 目的就是为了赢得更多的收入,至于真否,待SEOer 解答,正如文字所说,伪静态就是假的静态. 2.准备工作:下载Ur ...

  6. iOS分享 - 对象间的通信之delegate、notificationCenter与block

    在项目开发中,常常会涉及到对象之间的通信,而为了降低对象间的耦合,会采用delegate.notificationCenter.block三种方式来进行实现,对于他们的使用,也许大家都能熟练掌握,但是 ...

  7. NonUniqueObjectException 问题

    org.hibernate.NonUniqueObjectException: a different object with the same identifier value was alread ...

  8. 练习使用css3实现3d按钮

    网上有很多漂亮的用css3实现的3d按钮,如'这个'.‘糖果色按钮’, 今天练习了一下,喏,下面这样,兼容性不好. 小黑子小虎子 3d效果和发光效果都是利用box-shadow,也经常利用伪元素:af ...

  9. php打印xml格式数据

    在你要输出的xml前边加       header("Content-type:text/xml;charset=utf-8"); 这个上边不要有任何输出 打印用echo 别用va ...

  10. 百度PHP实习一面面试题-算法-二维有序矩阵的查找

    题目描述 有一个二维矩阵,每一行的元素,从左到右保持严格递增,每一列的元素,从上到下保持严格递增.查找给定元素elem,返回NULL或元素位置. 1 3 7 15 16 2 5 8 17 19 3 6 ...