2
我正在嘗試從自定義Web部件訪問SharePoint 2010列表中的項目。使用線程時,List.ItemCount屬性是準確的,但是項目集合是空的。有沒有人找到解決這個問題的方法?我的代碼訪問列表如下:是否可以使用Web部件中的多線程訪問SharePoint 2010列表?
protected override void RenderContents(System.Web.UI.HtmlTextWriter writer)
{
Thread wThread = new Thread(new ThreadStart(WriteW));
//only showing one thread for simplicity
wThread.Start();
Thread.Sleep(500);
while (threadcount > 0)
{
Thread.Sleep(400);
}
lblGreeting.RenderControl(writer);
}
public void WriteW()
{
lock (lockobject)
{
threadcount++;
}
SPSite spsConflictSite = new SPSite("http://myserver/mysite");
SPWeb spwConflictWeb = spsConflictSite.OpenWeb();
SPList splConflictList = spwConflictWeb.Lists["Thread Tester List"];
DataTable myTable = splConflictList.Items.GetDataTable();
lblGreeting.Text += " " + myTable.Rows[0]["Title"].ToString();
spsConflictSite.Dispose();
lock (lockobject)
{
threadcount--;
}
}
我不能說積極,我想聽到答案,因爲我有一個SP網絡部分,我很想多線程。但是,您在顯示的代碼中確實存在併發問題。當您在`WriteW`方法中鎖定'threadcount`成員變量的寫入時,您應該在`RenderContents`方法中讀取它時做同樣的事情。 – 2010-11-23 22:20:18