Click here to Skip to main content
15,881,139 members
Home / Discussions / C#
   

C#

 
QuestionDragDrop to a native window? Pin
TuringTest112-Jan-04 19:20
TuringTest112-Jan-04 19:20 
AnswerRe: DragDrop to a native window? Pin
Heath Stewart13-Jan-04 5:07
protectorHeath Stewart13-Jan-04 5:07 
GeneralRe: DragDrop to a native window? Pin
TuringTest113-Jan-04 5:55
TuringTest113-Jan-04 5:55 
Generalasking for system tray application Pin
azusakt12-Jan-04 16:25
azusakt12-Jan-04 16:25 
GeneralRe: asking for system tray application Pin
Jeff Mackie12-Jan-04 17:16
sussJeff Mackie12-Jan-04 17:16 
GeneralRe: asking for system tray application Pin
azusakt12-Jan-04 17:51
azusakt12-Jan-04 17:51 
GeneralRe: asking for system tray application Pin
Heath Stewart13-Jan-04 4:53
protectorHeath Stewart13-Jan-04 4:53 
Questionreturning when inside a lock - is it safe? Pin
Brandon Haase12-Jan-04 15:29
Brandon Haase12-Jan-04 15:29 
I've run into a situation I haven't seen before, and was wondering if someone with a bit more threading experience would like to chime in.

The class IsoCurrencyInfo maintains a collection of IsoCurrencyInfo singletons. The instances of IsoCurrencyInfo class themselves are read-only once created, so by themselves are thread-safe. The trick is keeping the ArrayList container in line. I think I have it nailed, but in doing so I am executing a return statement while inside a lock().

The code works very well under heavy concurrent load Cool | :cool: , but I can't help wondering "Is this safe?" Is there a better way to structure this method to optimize the locking?

I have adapted the Singleton implementation pattern[^] from Microsoft Patterns & Practices[^].

public static IsoCurrencyInfo GetInstance(string isoCode) 
{
	IsoCurrencyInfo newICI = null;

	if(null == _iciRegistry)
		lock(syncRoot) 
			if(null == _iciRegistry)
			{
				_iciRegistry = new ArrayList();

				newICI = new IsoCurrencyInfo(isoCode);
				_iciRegistry.Add(newICI);
			}
	if(null == newICI) 
		lock(syncRoot) 
		{
			foreach(IsoCurrencyInfo existingICI in _iciRegistry)
				if(existingICI.AlphaCode == isoCode)
					return existingICI;

			newICI = new IsoCurrencyInfo(isoCode);
			_iciRegistry.Add(newICI);
		}
	return newICI;
}

Thanks!
B
AnswerRe: returning when inside a lock - is it safe? Pin
Heath Stewart13-Jan-04 4:46
protectorHeath Stewart13-Jan-04 4:46 
GeneralRe: returning when inside a lock - is it safe? Pin
Brandon Haase13-Jan-04 16:13
Brandon Haase13-Jan-04 16:13 
AnswerRe: returning when inside a lock - is it safe? Pin
scadaguy13-Jan-04 5:23
scadaguy13-Jan-04 5:23 
GeneralRe: returning when inside a lock - is it safe? Pin
Brandon Haase13-Jan-04 16:28
Brandon Haase13-Jan-04 16:28 
Generalremoting access Pin
haih12-Jan-04 15:23
haih12-Jan-04 15:23 
GeneralRe: remoting access Pin
Heath Stewart13-Jan-04 4:31
protectorHeath Stewart13-Jan-04 4:31 
GeneralHelp with MonthCalendar class Pin
KingTermite12-Jan-04 15:18
KingTermite12-Jan-04 15:18 
GeneralRe: Help with MonthCalendar class Pin
Heath Stewart13-Jan-04 3:41
protectorHeath Stewart13-Jan-04 3:41 
GeneralRe: Help with MonthCalendar class Pin
KingTermite13-Jan-04 4:27
KingTermite13-Jan-04 4:27 
GeneralRe: Help with MonthCalendar class Pin
Heath Stewart13-Jan-04 4:37
protectorHeath Stewart13-Jan-04 4:37 
GeneralRe: Help with MonthCalendar class Pin
KingTermite13-Jan-04 5:43
KingTermite13-Jan-04 5:43 
GeneralRe: Help with MonthCalendar class Pin
Heath Stewart13-Jan-04 5:50
protectorHeath Stewart13-Jan-04 5:50 
GeneralRe: Help with MonthCalendar class Pin
KingTermite13-Jan-04 6:02
KingTermite13-Jan-04 6:02 
GeneralRe: Help with MonthCalendar class Pin
Heath Stewart13-Jan-04 6:47
protectorHeath Stewart13-Jan-04 6:47 
GeneralRe: Help with MonthCalendar class Pin
KingTermite15-Jan-04 14:49
KingTermite15-Jan-04 14:49 
GeneralRe: Help with MonthCalendar class Pin
Heath Stewart16-Jan-04 3:42
protectorHeath Stewart16-Jan-04 3:42 
GeneralFile Permissions Pin
krisp12-Jan-04 14:54
krisp12-Jan-04 14:54 

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.