是否可以在 Web 部件中使用多线程访问 SharePoint 2010 列表?
我正在尝试从自定义 Web 部件访问 SharePoint 2010 列表中的项目。使用线程时,List.ItemCount 属性是准确的,但项目集合为空。有没有人找到解决这个问题的方法?我访问列表的代码如下:
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--;
}
}
I am trying to access the items in a SharePoint 2010 list from a custom webpart. When using threading, the List.ItemCount property is accurate, but the item collection is empty. Has anyone found a way around this? My code for accessing the list is below:
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--;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为在线程中使用 Dispose 可能是一个问题。尝试将 SPSite 调用包装在 using 语句中,以使 .net 重新获得对处置的一些控制权。我已经在 SP2010 中使用了多线程,但它是一头猪,并且有很多“计划外的功能”。
我当时确实与微软打了一个支持电话,他们的回答是这应该是可能的,但不受支持。
I think using Dispose in the thread could be an issue. Try wrapping the SPSite call in a using statement to give .net back some control over the disposal. I have had multithreading in SP2010 working but it was a pig and had a lot of "unplanned features".
I did have a support call with Microsoft open at the time and their answer was that it should be possible but wasn't supported.