Hi Ryan,

This looks better than my original suggestion - however, this doesn't
allow for different combinations of parameters to be called in parallel:
this database call could take a few seconds, and the results depending
on the parameters will be different.  Perhaps I need a hashtable of lock
objects? ;-)

Best wishes
James


> -----Original Message-----
> From: Unmoderated discussion of advanced .NET topics. 
> [mailto:[EMAIL PROTECTED] On Behalf Of Heath Ryan
> Sent: 22 July 2004 10:10
> To: [EMAIL PROTECTED]
> Subject: Re: [ADVANCED-DOTNET] Multithreaded hash table access
> 
> Hi James,
> 
> the synchronized Hashtable only syncs the hashTable to sync 
> also the db access, try this one...
> 
> Hashtable ht = new Hashtable();
> 
> public DataTable GetList( int a, int b, int c) {
>   // lookup the dataTable
>   DataTable dt = (DataTable)ht[key];
> 
>   if ( dt == null)
>   {
>     // since our last check
>     // the dataTable might be put already
>     // by another thread, so lock and lookup again
>     lock( ht.SynRoot)
>     {
>       dt = (DataTable)ht[key];
>       if ( dt == null)
>       {
>         // insert new DataTable
>         dt = GetListLong(a, b, c);
>         ht[key] = dt;
>         // finished get out!
>         return dt;
>       }
>     }
>   }
> 
>   // dataTable exists, but is it still valid?
>   if ( Expired( dt))
>   {
>     // since our last check
>     // an updated dataTable might be put already
>     // by another thread, so lock and lookup again
>     lock( ht.SynRoot)
>     {
>       dt = (DataTable)ht[key];
>       if ( Expired( dt))
>       {
>         // insert updated DataTable
>         dt = GetListLong(a, b, c);
>         ht[key] = dt;
>       }
>     }
>   }
> 
>   return dt;
> }
> 
> HTH
> // Ryan

===================================
This list is hosted by DevelopMentorŪ  http://www.develop.com
Some .NET courses you may be interested in:

NEW! Guerrilla ASP.NET, 17 May 2004, in Los Angeles
http://www.develop.com/courses/gaspdotnetls

View archives and manage your subscription(s) at http://discuss.develop.com

Reply via email to