473,748 Members | 3,107 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Is this static method threadsafe?

Hello All,

Following is a static method. Can you please tell me if this is
threadsafe...wh y? If it is not threadsafe...wh y?

Thank you.

public static string GetCachedApplic ationWideObject (string HashtableKey)
{
//Cache["DefaultSampleQ Page"] holds the hashtable
if(Cache["DefaultSampleQ Page"]==null)
{
CacheApplicatio nWideObjects();
_sHTable = ((Hashtable)Cac he["DefaultSampleQ Page"]);
return ((string)HTable[HashtableKey]);
}
else
{
_sHTable = ((Hashtable)Cac he["DefaultSampleQ Page"]);
return ((string)HTable[HashtableKey]);
}
}
Jan 13 '06 #1
3 2176
I forgot to add this information:

_sHTable is a static variable which is like a const variable for the entire
application; so no problem if two threads access that information at the same
time.

But my only question is if the parameter 1 is passed by thread 1 and
parameter 2 is passed by thread 2 is there any possibility that thread 1 will
be returned thread 2's result and vice versa?

Thanks

"Diffident" wrote:
Hello All,

Following is a static method. Can you please tell me if this is
threadsafe...wh y? If it is not threadsafe...wh y?

Thank you.

public static string GetCachedApplic ationWideObject (string HashtableKey)
{
//Cache["DefaultSampleQ Page"] holds the hashtable
if(Cache["DefaultSampleQ Page"]==null)
{
CacheApplicatio nWideObjects();
_sHTable = ((Hashtable)Cac he["DefaultSampleQ Page"]);
return ((string)HTable[HashtableKey]);
}
else
{
_sHTable = ((Hashtable)Cac he["DefaultSampleQ Page"]);
return ((string)HTable[HashtableKey]);
}
}

Jan 13 '06 #2
If all you are ever doing is reading from the Cache, i fail to see where the
problem lies, because the data will always be the same.
It's when you might have another thread that attempts to WRITE to the same
Cache item that you could run into issues.

Take a look at the lock keyword. Better yet, read MVP Jon Skeet's articles
on the subject:

http://www.yoda.arachsys.com/csharp/threads/

Peter
--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Diffident" wrote:
Hello All,

Following is a static method. Can you please tell me if this is
threadsafe...wh y? If it is not threadsafe...wh y?

Thank you.

public static string GetCachedApplic ationWideObject (string HashtableKey)
{
//Cache["DefaultSampleQ Page"] holds the hashtable
if(Cache["DefaultSampleQ Page"]==null)
{
CacheApplicatio nWideObjects();
_sHTable = ((Hashtable)Cac he["DefaultSampleQ Page"]);
return ((string)HTable[HashtableKey]);
}
else
{
_sHTable = ((Hashtable)Cac he["DefaultSampleQ Page"]);
return ((string)HTable[HashtableKey]);
}
}

Jan 14 '06 #3
no its not threadsafe. you do not serialize access to the hashtable, so race
condiditons can occur. also two threads can run CacheApplicatio nWideObjects
at the same time (is it threadsafe?)

the treading issue you worried about should not be a problem if
CacheApplicatio nWideObjects is threadsafe.

-- bruce (sqlwork.com)


"Diffident" <Di*******@disc ussions.microso ft.com> wrote in message
news:26******** *************** ***********@mic rosoft.com...
I forgot to add this information:

_sHTable is a static variable which is like a const variable for the
entire
application; so no problem if two threads access that information at the
same
time.

But my only question is if the parameter 1 is passed by thread 1 and
parameter 2 is passed by thread 2 is there any possibility that thread 1
will
be returned thread 2's result and vice versa?

Thanks

"Diffident" wrote:
Hello All,

Following is a static method. Can you please tell me if this is
threadsafe...wh y? If it is not threadsafe...wh y?

Thank you.

public static string GetCachedApplic ationWideObject (string HashtableKey)
{
//Cache["DefaultSampleQ Page"] holds the hashtable
if(Cache["DefaultSampleQ Page"]==null)
{
CacheApplicatio nWideObjects();
_sHTable = ((Hashtable)Cac he["DefaultSampleQ Page"]);
return ((string)HTable[HashtableKey]);
}
else
{
_sHTable = ((Hashtable)Cac he["DefaultSampleQ Page"]);
return ((string)HTable[HashtableKey]);
}
}

Jan 14 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
4160
by: Clint | last post by:
Hey all - Excuse the cross-post ... I'm not sure what the appropriate newsgroup would be for this question. I have a question that I'm not quite sure how to ask. For all I know, I have the verbaige completely wrong, but here goes nothing ... I'm currently using the MS Data Access Block for a desktop application I'm writing. Recently, I had to add a call to a web service, which in
25
5173
by: Sahil Malik [MVP] | last post by:
So here's a rather simple question. Say in an ASP.NET application, I wish to share common constants as static variables in global.asax (I know there's web.config bla bla .. but lets just say I wanna use global.asax) --- Would you declare your static var as --- public static int x ;
2
2918
by: David | last post by:
I have a static method in the dll that looks like this public static int myStaticFunction(int input){ } My question is, should I use a mutex inside the function to ensure that the function is threadsafe? This is used for ASP.NET. I am asking this question without knowing how IIS works as a multi-threaded application. Does each websession get a thread? If so, if two users are calling the static function at the same time, do they
11
2254
by: dee | last post by:
OleDbCommand class like many .NET classes has the following description in its help file: "Thread Safety Any public static (Shared in Visual Basic) members of this type are safe for multithreaded operations. Any instance members are not guaranteed to be thread safe." I have 2 questions: 1. I thought dynamic variables are thread-safe since threads have their own
11
1913
by: Dave | last post by:
I'm trying to understand the implications of using static methods and properties in asp.net so I found an article "Troubleshooting ASP.NET applications with the use of static keywords" (http://support.microsoft.com/?id=893666) that discusses the possiblity of users seeing other users data if they access static methods at the same time because values are shared. Although I never used it, I noticed that some SqlHelper functions part of...
7
2399
by: Jon Vaughan | last post by:
I have a piece of code that I want to run on a Pocket Pc, I have written a data class that will store the small amount of data that is required for the program. As this class will be used via a few forms in the program, what the best way to setup this class - Static ? and if so where do I enter the setup for the initial fetch, as at the moment its in the constructor, i'm right in thinking there is no constructor in a static class. Thanks
3
4735
by: David Veeneman | last post by:
Why do these questions always come up on Friday afternoon? I'm starting to use GoF singleton classes in my projects. Right off the bat, I've run into a surprise. I thought that a Singleton could be implemented using a static constructor, using code like this, based on version #4 at http://www.yoda.arachsys.com/csharp/singleton.html: public sealed class MyClass { private static MyClass m_MyClass = new MyClass();
3
4359
by: Adam | last post by:
What happens if one thread is executing a static constructor and another thread starts. Does the second thread block until the first is done in the static constructor? I want to make sure all my globals are initialized and the second thread does not throw an exception. Thanks in advance. Adam Smith
12
3581
by: chandu | last post by:
hello, i want to know usage of static methods in a class. is it advantageous or disadvantage to use more static methods in a class. thank u
0
8826
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9316
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9241
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8239
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6793
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4597
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4867
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3303
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2211
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.