我有以下返回空值的方法,尝试对其进行调试,我发现到达最后一行(返回值)时,要返回的变量具有预期值,但没有退出该方法并返回值,代码会使用“ NullReferenceException”返回到前面的捕获中,并在此之后返回null。
仅当在多个线程中同时调用该方法时,这种情况才会发生。
有人可以向我解释为什么会这样吗?
调用块:
List<Thread> threads = new List<Thread>();
foreach (Event eV in temp)
{
var associatedUserEvent = new User_Event().GetItemByFilter("Event", eV.Title);
ThreadStart c = new ThreadStart(() => xx(temp2, eV));
Thread cT = new Thread(c);
threads.Add(cT);
cT.Start();
}
foreach (var cT in threads)
{
cT.Join();
}
调用方法:
public void xx(List<User_Event> temp2, Event eV)
{
var associatedUserEvent = new User_Event().GetItemByFilter("Event", eV.Title);
if (associatedUserEvent != null) temp2.Add(associatedUserEvent);
}
向后运行的方法: (字符串a,c和t仅用于调试目的)
public SPListItemCollection executeQuery()
{
SPListItemCollection toReturn = null;
string a, t, c;
try
{
SPWeb web = SPContext.Current.Web;
SPSecurity.RunWithElevatedPrivileges(delegate()
{
if (type.getListName() != null)
{
SPList list = web.GetList(web.ServerRelativeUrl + "/Lists/" + type.getListName());
if (filters != null || folderPath != null)
{
SPQuery query = new SPQuery();
if (customViewFields != null)
{
query.ViewFields = buildViewFields();
query.ViewFieldsOnly = true;
query.IncludeMandatoryColumns = false;
}
query.IncludeAttachmentUrls = true;
query.Folder = web.GetFolder(web.ServerRelativeUrl + "/Lists/" + type.getListName() + "/" + folderPath);
query.Query = this.filters != null && this.filters.Length > 0 ? this.CreateCAMLQuery() : @"<Query><OrderBy><FieldRef Name='ID' /></OrderBy></Query>";
query.ViewAttributes = "Scope=\"Recursive\"";
toReturn = list.GetItems(query);
a = "";
}
else
{
toReturn = list.GetItems(customViewFields);
}
c = "";
}
else
{
throw new System.ArgumentException("Custom attributte listname cannot be null");
}
t = "";
});
}
catch (Exception exception)
{
a = "";
}
return toReturn;
}