【发布时间】:2014-04-27 12:37:30
【问题描述】:
我正在用 C# (Visual Studio 2013) 编写 Windows 服务。 该服务为其连接的每个服务器激活一个线程。
“服务器”对象被定义为“ThreadStatic”(因此每个线程都应该将其作为不同的对象访问):
[ThreadStatic]
public static Server CurServer
在调试时 - 我有时会收到错误:
ArgumentOutOfRangeException
Index was out of range. Must be non-negative and less than the size of the collection.
它出现在以下行(在“服务器”线程的方法内):
EventConn.FindString = G.CurServer.FilesList[G.CurServer.nFilesIndex].SearchString;
但奇怪的是调试器显示的值是ok的:
G.CurServer.FilesList.count = 1
G.CurServer.nFilesIndex = 0
所以不应该有任何错误!!!
当我按 F11(调试器步骤)时,它会继续调试,好像一切都很好,并且分配也有效......
为什么???? :0
在分配当前线程的值之前提示错误的Visual Studio中是否存在错误?或者我没有使用线程 saftley(更有可能)?
【问题讨论】:
标签: c# multithreading indexing visual-studio-2013 threadstatic