|
||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
IntroductionCurrently, more and more companies are beginning to move to SharePoint 2007. As the basis of SharePoint applications, web parts need more attention since they are customized to business or user requirements. Let’s discuss some issues about how to upgrade an async web part from SharePoint 2003 to SharePoint 2007. BackgroundWhen developers recompile a web part which implements multiple threads rendered using It’s easy to implement First, let's walk through the source codeThe project contains three class files: the web part class (including how to implement the The web part classusing 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();
}
}
During The WebPartMultiThread definition classusing 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);
}
}
How to implement The WebPartMultiThread package classusing 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()
{
}
}
When you run this page, you see something like this screenshot:
HistoryThis is the first version.
|
|||||||||||||||||||||||||||||||||||