【问题标题】:Is it possible to access a SharePoint 2010 list using multi threading in a web part?是否可以在 Web 部件中使用多线程访问 SharePoint 2010 列表?
【发布时间】:2011-05-14 18:24:01
【问题描述】:

我正在尝试从自定义 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--;
        }
    }

【问题讨论】:

  • 我不能肯定地说,我想听听答案,因为我有一个我想多线程的 SP Web 部件。但是,您显示的代码中确实存在并发问题。当您在 WriteW 方法中锁定 threadcount 成员变量的写入时,您应该在 RenderContents 方法中对它的读取执行相同的操作。
  • 另外,虽然您确实是 Dispose-ing 的 spsConflictSite,但您应该对 spwConflictWebmyTable 做同样的事情,因为它们都实现了 IDisposable
  • 感谢 cmets。在大多数情况下,这是用于测试列表访问问题的真实程序的 hello world 版本。为了简单起见,我遗漏了很多内容。
  • 2 个问题:1) 不使用线程时是否有效? 2) URL 是否在 Web 部件将运行的上下文之外?
  • 1) 是的,没有线程它工作正常 2) 不。它在同一个网站集中。

标签: c# multithreading list sharepoint-2010 web-parts


【解决方案1】:

我认为在线程中使用 Dispose 可能是一个问题。尝试将 SPSite 调用包装在 using 语句中,以让 .net 重新控制处置。我在 SP2010 中使用了多线程,但它是一头猪,并且有很多“计划外的功能”。

当时我确实接到了 Microsoft 的支持电话,他们的回答是应该可以,但不受支持。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-05-24
    • 2011-10-13
    • 2013-08-20
    • 2012-07-15
    • 2010-09-17
    • 1970-01-01
    • 2011-05-11
    • 1970-01-01
    相关资源
    最近更新 更多