Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / ASP.NET

Multithread in MOSS page by implementing PageAsyncTask

4.80/5 (4 votes)
24 Apr 2008CPOL1 min read 1   162  
To implement PageAsyncTask for web part rendering in parallel instead of RegisterWorkItem.

Introduction

Currently, 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.

Background

When developers recompile a web part which implements multiple threads rendered using Microsoft.SharePoint.WebPartPages.WebPart.RegisterWorkItem, they would find a warning: 'Microsoft.SharePoint.WebPartPages.WebPart.RegisterWorkItemCallback(System.Threading.WaitCallback, object)' is obsolete: 'Use Page.RegisterAsyncTask instead.'.

It’s easy to implement Page.RegisterAsyncTask, just like MSDN says. The challenge is how to make it work in the SharePoint web part page…

First, let's walk through the source code

The project contains three class files: the web part class (including how to implement the WebPartMultiThread method and set time out messages), the WebPartMultiThread definition class, and the WebPartMultiThread package class.

The web part class

C#
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();
    }
}

During OnPreRender, we create a new instance of our WebPartMultiThread class and give it a threadTimeout to decide when the time expires. Then, we invoke RenderWorkItemTimeout (this method is not Microsoft.Sharepoint.WebPartPage.WebPart.RenderWorkItemTimeOut) to start rendering the web part (supposed to consume 5 seconds per web part) in parallel to achieving the performance. During RenderWebPart, we will render different contents depending on whether it was expired or not.

The WebPartMultiThread definition class

C#
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);
    }
}

How to implement OnTaskBeginWithWaitCallback() is an important part of this project. It needs an instance of WebPartMultiThreadPacket.

The WebPartMultiThread package class

C#
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()
    {
    }
}

When you run this page, you see something like this screenshot:

Image 1

History

This is the first version.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)