从C#线程回调检索值时出错

从C#线程回调检索值时出错,c#,multithreading,C#,Multithreading,我有下面一段代码来生成两个线程,如方法2中所示 Thread[] arrThreads = new Thread[2]; Thread t1 = new Thread(() => { value1 = this.Method1(<some params>); }); t1.Start(); arrThreads[0] = t1; Thread t2 = new Thread(() => { value2 = this.SomeOtherMethod(&

我有下面一段代码来生成两个线程,如方法2中所示

Thread[] arrThreads = new Thread[2];

Thread t1 = new Thread(() =>
{
    value1 = this.Method1(<some params>);
});
t1.Start();
arrThreads[0] = t1;

Thread t2 = new Thread(() =>
{
    value2 = this.SomeOtherMethod(<some params>);
});
t2.Start();
arrThreads[1] = t2;

// Code which waits for both threads to get over OR for a threshold, whichever comes first!
Thread[]arrThreads=新线程[2];
线程t1=新线程(()=>
{
value1=this.Method1();
});
t1.Start();
arr线程[0]=t1;
线程t2=新线程(()=>
{
value2=this.SomeOtherMethod();
});
t2.Start();
[1]=t2;
//等待两个线程都超过或等待阈值的代码,以先到者为准!
这在我的开发人员身上运行良好。但在发布到IIS生产机器时,遇到以下错误

    at System.Data.RBTree`1.GetNodeByIndex(Int32 userIndex) 
    at System.Data.RBTree`1.get_Item(Int32 index) 
    at System.Data.DataRowCollection.get_Item(Int32 index) 
    at Namespace.Class.Method1(params) in D:\XXXX\Class.cs:line 11309 
    at Namespace.Class.<>c__DisplayClasse.<Method2>b__a() in D:\XXXX\Class.cs:line 11687 
    at System.Threading.ExecutionContext.runTryCode(Object userData) 
    at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData) 
    at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ThreadHelper.ThreadStart()
at System.Data.RBTree`1.GetNodeByIndex(Int32用户索引)
在System.Data.RBTree`1.get_项(Int32索引)
位于System.Data.DataRowCollection.get_项(Int32索引)
在D:\XXXX\Class.cs中的Namespace.Class.Method1(params)处:第11309行
在D:\XXXX\Class.cs:第11687行的Namespace.Class.c\u DisplayClasse.b\u a()处
位于System.Threading.ExecutionContext.runTryCode(对象用户数据)
在System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode代码、CleanupCode backoutCode、Object userData)中运行
在System.Threading.ThreadHelper.ThreadStart()上运行(ExecutionContext ExecutionContext,ContextCallback回调,对象状态)

有人能帮我解决这个错误吗?提前感谢。

您需要检查
名称空间.Class.Method1
D:\XXXX\Class.cs:line
11309上或前后(有时行号跟踪有点扭曲)发生的情况。我预计它将采取以下行动:

var row = someDataTable.Rows[0];

此时,没有行-可能是由于某种原因,查询没有返回任何数据(请检查查询)。从理论上讲,这也可能与非同步线程相关(因为您启动了两个线程),或者与争用条件相关——但是,我们没有足够的上下文。在此之前,没有理由假定这与多线程有关,但如果有多个线程对相同的状态数据进行变异,则需要记住这是一种可能性。

异常的
。消息是什么?您显示的是stacktrace,而不是消息此处是IIS管理事件记录的内容。发生了未处理的异常,进程被终止。应用程序ID:/LM/W3SVC/8/根进程ID:2756异常:System.IndexOutOfRangeException消息:位置0处没有行。我可以看到两种可能性:1)您的代码未正确处理空结果集,2)您的两个方法不是独立的,并且试图错误地使用共享数据。无论哪种方法,您都应该使用任务或线程池,而不是线程。@HenkHolterman meh,不确定我们是否有足够的信息来说明这一点。谢谢您的回复。Method1在第11309行中真正拥有的是一个简单的}(函数末尾的花括号)。此外,上面的堆栈跟踪和消息来自事件查看器管理事件。我还要指出我的应用程序池有4个工作进程。这两个方法获取完全不同的对象,因此不会像您提到的那样改变相同的状态。@JunaidKirkire工作进程是不相关的;如果这些方法在不同的对象上工作,那么多线程是一种危险的做法。正如我所说,行号可以是“on或about”-在该方法中的某个地方,有某种东西通过索引访问数据表中的某一行(行
0
,或者显式
0
,或者在
for
循环中)。这里的问题是您的代码-您需要了解为什么该表是空的,并修复它。