📄 global.asax
字号:
<%@ Import namespace="System.Threading" %>
<%@ Import namespace="System.Text" %>
<Script language="C#" runat=server>
MyAsyncResult asyncResult;
public override void Init(){
BeginEventHandler beginEvent = new BeginEventHandler(Begin);
EndEventHandler endEvent = new EndEventHandler(End);
AddOnBeginRequestAsync(beginEvent, endEvent);
}
// Begin Event Handler
public IAsyncResult Begin(Object source, EventArgs e, AsyncCallback callback, Object extraData) {
asyncResult = new MyAsyncResult(this.Context, callback, extraData);
return asyncResult;
}
// End Event Handler
public void End (IAsyncResult ar){
}
// Async implementation class
internal class MyAsyncResult : IAsyncResult {
AsyncCallback callback;
object asyncState;
Thread thread;
HttpContext context;
bool isCompleted = false;
internal MyAsyncResult(HttpContext context, AsyncCallback callback, object asyncState){
this.callback = callback;
this.asyncState = asyncState;
this.context = context;
thread = new Thread(new ThreadStart(CallStockQuoteWebService));
thread.Start();
}
public object AsyncState {
get {
return asyncState;
}
}
public WaitHandle AsyncWaitHandle {
get {
return null;
}
}
public bool CompletedSynchronously {
get {
return false;
}
}
public bool IsCompleted {
get {
return isCompleted;
}
}
public void QuoteCallBack(IAsyncResult ar) {
QuoteService quote = new QuoteService();
QuoteDetails[] d = quote.EndGetQuotes(ar);
context.Application["QuoteDetails"] = d;
isCompleted = true;
callback(this);
}
public void CallStockQuoteWebService() {
QuoteService quote = new QuoteService();
quote.BeginGetQuotes(new AsyncCallback(QuoteCallBack), null);
}
}
</Script>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -