越来越多的应用迁移到MOSS上,作为其基本应用元件的WebPart自然得到最大最多的关注了,因此如何编写高性能高的WebPart 也越来越急迫了,利用asp.net2.0基本的回调功能支持,可以在WebPart中实现多线程的效果哦。
我们在SPS2003的时候使用:Microsoft.SharePoint.WebPartPages.WebPart.RegisterWorkItem
MOSS 2007 自然升级到:Page.RegisterAsyncTask
关于 Page.RegisterAsyncTask可以参考MSDN:http://msdn.microsoft.com/en-us/library/system.web.ui.page.registerasynctask.aspx 介绍,下面来看看基本的实现代码:
定义了3个类:
WebPartMultiThread
class
WebPartMultiThread
definition class
WebPartMultiThread
package class
WebPartMultiThread
class:
Code
using System;
using System.Web;
using System.Web.UI;
using System.Threading;
using System.Xml.Serialization;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebPartPages;
public class WorkItemSample :
Microsoft.SharePoint.WebPartPages.WebPart
{
string log = "";
ManualResetEvent mre = new ManualResetEvent(false);
bool threadTimeOut = false;
WebPartMultiThread MultiThread;
public WorkItemSample()
{
}
protected override void OnPreRender(EventArgs e)
{
log += "before register: " +
DateTime.Now.ToLongTimeString() + "<BR>";
MultiThread = new WebPartMultiThread(threadTimeOut, this.Page);
MultiThread.RegisterWorkItemCallback(new WaitCallback(DoWork), null);
//DoWork(null);
log += "after register: " +
DateTime.Now.ToLongTimeString() + "<BR>";
}
protected override void RenderWebPart(HtmlTextWriter output)
{
if (MultiThread != null && MultiThread.ThreadTimeOut)
{
string TimeoutMessage = "Timed out";
MultiThread.RenderWorkItemTimeout(output, TimeoutMessage);
}
else
{
log += "RenderWebPart: " +
DateTime.Now.ToLongTimeString() + "<BR>";
output.Write(log);
}
}
// Sleep for 5 seconds to simulate doing work
private void DoWork(object o)
{
log += "before DoWork: " + DateTime.Now.ToLongTimeString() +
this.Title + "<BR>";
Thread.Sleep(7000);
log += "After DoWork: " + DateTime.Now.ToLongTimeString() +
this.Title + "<BR>";
mre.Set();
}
}
The WebPartMultiThread definition class:
Code
using System;
using System.Web;
using System.Web.UI;
using System.Threading;
using System.Xml.Serialization;
public class WebPartMultiThread
{
private bool _threadtimeout;
private Page _page;
public bool ThreadTimeOut
{
set
{
_threadtimeout = value;
}
get
{
return _threadtimeout;
}
}
public System.Web.UI.Page myPage
{
set
{
_page = value;
}
get
{
return _page;
}
}
public WebPartMultiThread(bool pThreadTimeOut, Page pPage)
{
ThreadTimeOut = pThreadTimeOut;
myPage = pPage;
}
public bool RegisterWorkItemCallback(WaitCallback beginCallBack, object state)
{
if (!this.ThreadTimeOut)
{
WebPartMultiThreadPacket pa = new WebPartMultiThreadPacket();
pa.state = state;
pa.beginCallBack = beginCallBack;
PageAsyncTask task = new PageAsyncTask(new BeginEventHandler(
this.OnTaskBeginWithWaitCallback),
new EndEventHandler(this.OnTaskEnd),
new EndEventHandler(this.OnTaskTimeout), pa, true);
if (this.myPage != null)
{
this.myPage.RegisterAsyncTask(task);
}
}
else
return false;
return true;
}
private IAsyncResult OnTaskBeginWithWaitCallback(object sender,
EventArgs e, AsyncCallback cb, object o)
{
return ((WebPartMultiThreadPacket)o).beginCallBack.BeginInvoke(
((WebPartMultiThreadPacket)o).state, cb, null);
}
private void OnTaskEnd(IAsyncResult ar)
{
}
private void OnTaskTimeout(IAsyncResult ar)
{
this.ThreadTimeOut = true;
}
public void RenderWorkItemTimeout(HtmlTextWriter output, string outputMessage)
{
output.Write(outputMessage);
}
}
The WebPartMultiThread package class:
Code
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Xml.Serialization;
public class WebPartMultiThreadPacket
{
public WaitCallback beginCallBack;
public object state;
public WebPartMultiThreadPacket()
{
}
}
我们通过模拟耗时操作:Thread.Sleep(7000);来达到异步多处理请求效果,自然得到相当好的用户体验。
此处下载示例代码