我正在嘗試爲網站集中的所有網絡運行SharePoint的SPSiteDataQuery
以獲取列表集合。從一個列表[spWeb.GetSiteData(dataQuery)
]的所有站點獲取數據的平均時間爲1到20秒之間的任意值。列表集合實際上可以包含任意數量的列表。線程在ASMX Web服務中的副作用
我已經實現瞭如下的線程邏輯。但是,我在每次通話中獲得不同數量的記錄。我對多線程體系結構並不熟悉。發生了什麼事情,值得我思考一個更好的線程架構嗎?
private static Dictionary<string,string> _workTypes = new Dictionary<string, string>();
private static Dictionary<string,string> _workspaces = new Dictionary<string, string>();
private static List<string> _processedMyWorkItems = new List<string>();
private static readonly object Locker = new object();
var threads = new List<Thread>();
var processedMyWorkData = new Dictionary<string, List<XElement>>();
foreach (string selectedList in selectedLists.Distinct().OrderBy(l => l))
{
string theSelectedList = selectedList;
string listIds = string.Empty;
foreach (string listId in GetListIdsFromDb(selectedList, spWeb, archivedWebs)
.Where(listId => !selectedListIds.Contains(listId)))
{
selectedListIds.Add(listId);
listIds += string.Format(@"<List ID='{0}'/>", listId);
}
if (string.IsNullOrEmpty(listIds)) continue;
dataQuery.Lists = string.Format("<Lists MaxListLimit='0'>{0}</Lists>", listIds);
var thread = new Thread(() =>
{
List<XElement> processedMyWork = ProcessMyWork(selectedFields, spSite, spWeb, dataQuery, fieldTypes);
lock (Locker)
{
processedMyWorkData.Add(theSelectedList, processedMyWork);
}
}) { Name = theSelectedList };
threads.Add(thread);
thread.Start();
}
bool done = false;
while (!done)
{
Thread.Sleep(500);
bool noThreadIsAlive = true;
var threadsCompleted = new List<Thread>();
foreach (var thread in threads)
{
if (thread.IsAlive)
{
noThreadIsAlive = false;
}
else
{
threadsCompleted.Add(thread);
}
}
foreach (var thread in threadsCompleted)
{
threads.Remove(thread);
string key = thread.Name;
foreach (var xElement in processedMyWorkData[key])
{
result.Element("MyWork").Add(xElement);
}
lock (Locker)
{
processedMyWorkData.Remove(key);
}
}
done = noThreadIsAlive;
}
的TPL(任務並行庫)使這樣的工作更容易(或至少更容易閱讀)比手動使用線程。 –
你在什麼版本的.NET?似乎你可以從使用.NET 4.0中的任務中獲益 –
@JamesMichaelHare:我同意。但是,SharePoint無法處理.Net 4.0,但! – Moon