从下面的代码你可以知道它是存异步的Ajax,objAjax.onreadystatechange =
new OnReadyStateChange(objAjax, new EventHandler(OnReadyStateChange))
与JScript代码ajax.onreadystatechange = function() { };本质是差不多的,只不过
我使用的方法是它内部的形式。
public class Program { public static void Main(string[] args) { dynamic objAjax = Activator.CreateInstance(Type.GetTypeFromProgID("Msxml2.XMLHTTP")); objAjax.open("GET", "http://blog.csdn.net/u012395622", true); objAjax.onreadystatechange = new OnReadyStateChange(objAjax, new EventHandler(OnReadyStateChange)); objAjax.send(null); Console.ReadKey(false); } public static void OnReadyStateChange(object sender, EventArgs e) { dynamic objAjax = sender; if (objAjax.readyState == 4 || objAjax.status == 200) Console.WriteLine("success\r\n" + objAjax.responseText); else Console.WriteLine("fail"); } }
有很多人在C#使用Ajax没有办法去添加回调函数
但是他们却很聪明的采取了另一种办法,及ajax.waitForResponse(timeoutInSeconds)
Wait for asynchronous send to complete, with optional timeout.
一般都是提供INFINITE = -1不过这样做会阻塞线程,但是不会阻塞UI记得几年前有人
做过这个实验,不顾当时它是用的Winhttp / COM对象,说到Winhttp我倒是有一篇帖
子是封装的Winhttp / COM示例代码是包含简单的一个短点续传下载的法子,你们倒是
可以看看虽然肯定会有些人不削 不过本人并不在意 易语言 排斥语言可不是好程序员。
http://bbs.125.la/forum.php?mod=viewthread&tid=13771462 不过我既然说到是异步
方式那么肯定会是异步,否则不是打了我自己的脸不是,呵呵 总是会感到那么的愉悦?
using System; using System.Reflection; using System.Globalization;
public class OnReadyStateChange : IReflect { private volatile object m_sender; private volatile IReflect m_reflect; private volatile EventHandler m_event; public OnReadyStateChange(object sender, EventHandler eventHandler) { this.m_sender = sender; this.m_event = eventHandler; this.m_reflect = typeof(OnReadyStateChange); } Type IReflect.UnderlyingSystemType { get { return this.m_reflect.UnderlyingSystemType; } } FieldInfo IReflect.GetField(string name, BindingFlags bindingAttr) { return this.m_reflect.GetField(name, bindingAttr); } FieldInfo[] IReflect.GetFields(BindingFlags bindingAttr) { return this.m_reflect.GetFields(bindingAttr); } MemberInfo[] IReflect.GetMember(string name, BindingFlags bindingAttr) { return this.m_reflect.GetMember(name, bindingAttr); } MemberInfo[] IReflect.GetMembers(BindingFlags bindingAttr) { return this.m_reflect.GetMembers(bindingAttr); } MethodInfo IReflect.GetMethod(string name, BindingFlags bindingAttr) { return this.m_reflect.GetMethod(name, bindingAttr); } MethodInfo IReflect.GetMethod(string name, BindingFlags bindingAttr, Binder binder, Type[] types, ParameterModifier[] modifiers) { return this.m_reflect.GetMethod(name, bindingAttr, binder, types, modifiers); } MethodInfo[] IReflect.GetMethods(BindingFlags bindingAttr) { return this.m_reflect.GetMethods(bindingAttr); } PropertyInfo[] IReflect.GetProperties(BindingFlags bindingAttr) { return this.m_reflect.GetProperties(bindingAttr); } PropertyInfo IReflect.GetProperty(string name, BindingFlags bindingAttr) { return this.m_reflect.GetProperty(name, bindingAttr); } PropertyInfo IReflect.GetProperty(string name, BindingFlags bindingAttr, Binder binder, Type returnType, Type[] types, ParameterModifier[] modifiers) { return this.m_reflect.GetProperty(name, bindingAttr); } object IReflect.InvokeMember(string name, BindingFlags invokeAttr, Binder binder, object target, object[] args, ParameterModifier[] modifiers, CultureInfo culture, string[] namedParameters) { if(name == "[DISPID=0]" && this.m_event != null) this.m_event(this.m_sender, EventArgs.Empty); return this.m_reflect.InvokeMember(name, invokeAttr, binder, target, args, modifiers, culture, namedParameters); } }
时间: 2024-11-05 18:41:15