I했습니다 빼 안전 수집 목록을 사용하는 등 기능, 또한 잠금 연산자 사용 스레드 안전을 위해 : 나는 그것을 할로 변환 할사용하여 스레드 안전 ConcurrentDictionary 수집
public void ShowData(ref DataGridView dmRequests, ref DataGridView URL, ref DataGridView dmData, ref DataGridView errorCodes)
{
List<KeyValuePair<string, ulong>> dmReqList = new List<KeyValuePair<string, ulong>>();
List<KeyValuePair<string, ulong>> urlReqList = new List<KeyValuePair<string, ulong>>();
List<KeyValuePair<string, ulong>> dmDataList = new List<KeyValuePair<string, ulong>>();
List<KeyValuePair<string, ulong>> errCodesList = new List<KeyValuePair<string, ulong>>();
lock (m_logStruct.domainName)
{
dmReqList = m_logStruct.domainName.ToList();
}
lock(m_logStruct.URL)
{
urlReqList = m_logStruct.URL.ToList();
}
lock(m_logStruct.domainData)
{
dmDataList = m_logStruct.domainData.ToList();
}
lock(m_logStruct.errorCodes)
{
errCodesList = m_logStruct.errorCodes.ToList();
}
dmRequests.DataSource = dmReqList.OrderBy(x => x.Key).ToList();
URL.DataSource = urlReqList.OrderBy(x => x.Key).ToList();
dmData.DataSource = dmDataList.OrderBy(x => x.Key).ToList();
errorCodes.DataSource = errCodesList.OrderBy(x => x.Key).ToList();
}
을 자물쇠가 없다. 그것을 위해 내 기능이 너무보고 시작, 그래서 대신 목록 모음의 기능 ConcurrentDictionary 수집에 사용했습니다
public void ShowData(ref DataGridView dmRequests, ref DataGridView URL, ref DataGridView dmData, ref DataGridView errorCodes)
{
try
{
ConcurrentDictionary<string, ulong> dmReqList = new ConcurrentDictionary<string, ulong>();
ConcurrentDictionary<string, ulong> urlReqList = new ConcurrentDictionary<string, ulong>();
ConcurrentDictionary<string, ulong> dmDataList = new ConcurrentDictionary<string, ulong>();
ConcurrentDictionary<string, ulong> errCodesList = new ConcurrentDictionary<string, ulong>();
dmReqList = m_logStruct.domainName;
urlReqList = m_logStruct.URL;
dmDataList = m_logStruct.domainData;
errCodesList = m_logStruct.errorCodes;
dmRequests.DataSource = dmReqList.OrderBy(x => x.Key);
URL.DataSource = urlReqList.OrderBy(x => x.Key).ToList();//I get error here: Index is out of range
dmData.DataSource = dmDataList.OrderBy(x => x.Key).ToList();
errorCodes.DataSource = errCodesList.OrderBy(x => x.Key).ToList();
}
catch(IOException e)
{
MessageBox.Show(e + " Something bad has been occurred here!");
}
}
을하지만 시작이 기능에 (인덱스가 범위를 벗어나) 오류가 발생합니다. 스레드 안전 컬렉션 (ConcurrentDictionary) 올바르게 사용하는 방법? 내 오류를 해결하는 방법?
새 사전을 선언했지만 immedatly 덮어 씁니다. 당신은 당신의 모든 새로운 ConcurrentDictionary();'을 제거 할 수 있습니다, 그들은 당신을 위해 아무것도하지 않습니다. –
샘플은 여러 스레드 (모든 로컬 변수)에 변수를 노출 할 수없는 코드를 보여줍니다. 코드를 업데이트하여 다중 스레드에서'errCodesList '와 같은 것을 실제로 보여줄 수 있습니다. –
사이드 노트 : 샘플을 하나의 콜렉션 세트로 줄이는 것을 고려하십시오. 각 기능에 거의 동일한 4 개의 섹션이 추가 정보를 제공하지는 않습니다. –