我需要使用 Interlocked 类处理我的 C# 应用程序中的锁。我有这样的代码:
class LogStruct
{
public Dictionary<string, ulong> domainName;
public Dictionary<string, ulong> URL;
public Dictionary<string, ulong> domainData;
public Dictionary<string, ulong> errorCodes;
public LogStruct()
{
domainName = new Dictionary<string, ulong> { };
URL = new Dictionary<string, ulong> { };
domainData = new Dictionary<string, ulong> { };
errorCodes = new Dictionary<string, ulong> { };
}
}
class CLogParser
{
string domainName = parameters[0];
string errorCode = matches[1].Value;
LogStruct m_logStruct;
...
public CLogParser()
{
m_logStruct = new LogStruct();
}
...
public void ThreadProc(object param)
{
if (m_logStruct.errorCodes.ContainsKey(fullErrCode))
{
lock (m_logStruct.errorCodes)
{
m_logStruct.errorCodes[fullErrCode]++;
}
}
}
}
当我想在 Interlocked 类上替换 ThreadProc 中的锁时,例如:
public void ThreadProc(object param)
{
if (m_logStruct.errorCodes.ContainsKey(fullErrCode))
{
Interlocked.Increment(m_logStruct.errorCodes[fullErrCode]);
}
}
我收到此错误:
Error CS1502: The best overloaded method match for
`System.Threading.Interlocked.Increment(ref int)'
has some invalid arguments (CS1502) (projectx)
这个错误:错误 CS1503: Argument #1' cannot convert
ulong to ref int' (CS1503) (projectx)
如何解决?