我有一个非常详细的字典,它存储 350 个键,每个键都是 3 个字节的字节数组。该字典用于解释来自自制硬件的传入数据。
在对应用程序进行压力测试时,我遇到了一个奇怪的错误,它告诉我有一个KeyNotFoundException
. 奇怪的是字典里有钥匙,而我正盯着它看。字典使用了一个特殊的比较器,这可能是问题的原因:
private static Dictionary<byte[ ], string> PlayerMap =
new Dictionary<byte[ ], string>( new ByteArrayComparator( ) );
public class ByteArrayComparator : IEqualityComparer<byte[ ]> {
public bool Equals( byte[ ] left, byte[ ] right ) {
if ( left == null || right == null )
return left == right;
return left.SequenceEqual( right );
}
public int GetHashCode( byte[ ] Key ) {
if ( Key == null )
throw new ArgumentNullException( "Key" );
return Key.Sum( B => B );
}
}
这只发生在我模拟用户以疯狂的速度按下按钮时,这意味着字典被多次查询键。
为什么会这样?比较器有问题吗?
编辑
为了清楚起见,控制器异步运行(因为它需要能够处理来自多个不同来源的传入数据)。我不想发布控制器的所有源代码,因为有很多,其中一些是敏感的,但我将发布这两个处理控件初始化的方法和它侦听传入数据的方法:
private static void Initialize(
string DevicePath,
R3Controller.HIDP_CAPS Capabilities,
R3Controller.HIDD_ATTRIBUTES Attributes ) {
R3Controller.hRead = R3Controller.OpenDevice( DevicePath );
R3Controller.hWrite = R3Controller.OpenDevice( DevicePath );
R3Controller.fsRead = new FileStream( hRead, FileAccess.ReadWrite, Capabilities.OutputReportByteLength, false );
R3Controller.fsWrite = new FileStream( hWrite, FileAccess.ReadWrite, Capabilities.OutputReportByteLength, false );
if ( R3Controller.fsRead.CanRead ) {
R3Controller.barData = new byte[R3Controller.devCapabilities.Value.InputReportByteLength];
if ( R3Controller.fsRead.CanRead )
R3Controller.fsRead.BeginRead( R3Controller.barData, 0, R3Controller.barData.Length,
new AsyncCallback( R3Controller.Listen ), R3Controller.barData );
else
throw new Exception( "R3 Controller Can't Read Incoming Data" );
}
}
private static void Listen( IAsyncResult IAR ) {
R3Controller.fsRead.EndRead( IAR );
if ( R3Controller.fsRead.CanRead )
R3Controller.fsRead.BeginRead( R3Controller.barData, 0, R3Controller.barData.Length,
new AsyncCallback( R3Controller.Listen ), R3Controller.barData );
else
throw new Exception( "R3 Controller Can't Read Incoming Data" );
R3InputEventArgs Args = new R3InputEventArgs( R3Controller.barData );
if ( R3Controller.Locked || R3Controller.LockedControllers.Contains( Args.Controller ) ) {
//Respond to locked presses if necessary...
if ( R3Controller._LockedFeedback != null )
R3Controller._LockedFeedback( null, Args );
/*GetInvocationList( ).ToList( ).ForEach( E => (
E.Clone( ) as EventHandler<R3InputEventArgs> ).BeginInvoke( null, Args, R3Controller.Heard, null ) );*/
} else if ( R3Controller._ButtonPressed != null )
R3Controller._ButtonPressed(null, Args);/*.GetInvocationList( ).ToList( ).ForEach(
E => ( E.Clone( ) as EventHandler<R3InputEventArgs> ).BeginInvoke( null, Args, R3Controller.Heard, null ) );*/
}