Click here to Skip to main content
15,880,956 members
Home / Discussions / C#
   

C#

 
AnswerRe: C# Performance Issue Pin
Richard MacCutchan17-Aug-14 20:41
mveRichard MacCutchan17-Aug-14 20:41 
GeneralRe: C# Performance Issue Pin
Mycroft Holmes17-Aug-14 20:59
professionalMycroft Holmes17-Aug-14 20:59 
GeneralRe: C# Performance Issue Pin
Richard MacCutchan17-Aug-14 21:34
mveRichard MacCutchan17-Aug-14 21:34 
GeneralRe: C# Performance Issue Pin
SledgeHammer0118-Aug-14 4:46
SledgeHammer0118-Aug-14 4:46 
AnswerRe: C# Performance Issue Pin
Rob Philpott17-Aug-14 23:45
Rob Philpott17-Aug-14 23:45 
AnswerRe: C# Performance Issue Pin
Richard Deeming18-Aug-14 2:30
mveRichard Deeming18-Aug-14 2:30 
GeneralRe: C# Performance Issue Pin
SledgeHammer0118-Aug-14 4:51
SledgeHammer0118-Aug-14 4:51 
GeneralRe: C# Performance Issue Pin
Richard Deeming18-Aug-14 7:31
mveRichard Deeming18-Aug-14 7:31 
Your second example should look something like:
C#
theValue = concurrentDict.GetOrAdd(theKey, key =>
{
    DoSomeOtherStuff();
    DoAFewMoreThings();
    
    var theNewObject = NewUpAnObject(key);
    return theNewObject;
});

The entire delegate would be executed within the scope of a lock, as with your non-generic dictionary code. However, the lock would be more granular, based on the number of buckets and the concurrency level of the collection.

If you wanted to execute the "other stuff" outside of the lock, your code would look like:
C#
if (!concurrentDict.TryGetValue(theKey, out theValue))
{
    DoSomeOtherStuff();
    DoAFewMoreThings();
    
    var theNewObject = NewUpAnObject(theKey);
    theValue = concurrentDict.GetOrAdd(theKey, theNewObject);
    
    // Or:
    // theValue = concurrentDict.GetOrAdd(theKey, key => NewUpAnObject(key));
}

In this case, DoSomeOtherStuff, DoAFewMoreThings and NewUpAnObject would run outside of any lock. However, the insert to the dictionary would not suffer from a race condition, as the class is specifically written to cope with this type of code.



"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer


GeneralRe: C# Performance Issue Pin
SledgeHammer0118-Aug-14 8:31
SledgeHammer0118-Aug-14 8:31 
AnswerRe: C# Performance Issue Pin
Pete O'Hanlon18-Aug-14 3:09
mvePete O'Hanlon18-Aug-14 3:09 
GeneralRe: C# Performance Issue Pin
SledgeHammer0118-Aug-14 4:52
SledgeHammer0118-Aug-14 4:52 
GeneralRe: C# Performance Issue Pin
Pete O'Hanlon18-Aug-14 4:56
mvePete O'Hanlon18-Aug-14 4:56 
AnswerRe: C# Performance Issue Pin
jschell18-Aug-14 11:51
jschell18-Aug-14 11:51 
QuestionC#/JS problematic iteration with table... Pin
OmegaExtern16-Aug-14 17:23
OmegaExtern16-Aug-14 17:23 
AnswerRe: C#/JS problematic iteration with table... Pin
Richard MacCutchan16-Aug-14 21:11
mveRichard MacCutchan16-Aug-14 21:11 
QuestionC# Keybind for Autoclicker Pin
Aaudiio16-Aug-14 16:39
Aaudiio16-Aug-14 16:39 
AnswerRe: C# Keybind for Autoclicker Pin
Zain Ul Abidin16-Aug-14 19:49
Zain Ul Abidin16-Aug-14 19:49 
AnswerRe: C# Keybind for Autoclicker Pin
OmegaExtern16-Aug-14 20:44
OmegaExtern16-Aug-14 20:44 
GeneralRe: C# Keybind for Autoclicker Pin
Aaudiio16-Aug-14 20:47
Aaudiio16-Aug-14 20:47 
GeneralRe: C# Keybind for Autoclicker Pin
OmegaExtern16-Aug-14 20:50
OmegaExtern16-Aug-14 20:50 
Questioncheck array value exists Pin
scottichrosaviakosmos16-Aug-14 3:11
scottichrosaviakosmos16-Aug-14 3:11 
AnswerRe: check array value exists Pin
onelopez16-Aug-14 3:34
onelopez16-Aug-14 3:34 
GeneralRe: check array value exists Pin
scottichrosaviakosmos16-Aug-14 4:03
scottichrosaviakosmos16-Aug-14 4:03 
AnswerRe: check array value exists Pin
OriginalGriff16-Aug-14 3:39
mveOriginalGriff16-Aug-14 3:39 
QuestionCatching exceptions on backgroundworker Pin
pjank4215-Aug-14 19:44
pjank4215-Aug-14 19:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.