Static class accessed from many threads of different types

I have one single datatable object in a static class.

i want multiple threads of different classes themselves to be able to access this datatable through the static classe's single public static property.

i dont think reads are a problem(correct me if I am wrong)
but I am concerned about writes

if it were threads all of the same class type accessing it i could use monitor.enter and monitor.exit
but unfortunatly the threads come from different classes

solutions please?
joshuadavidleeAsked:
Who is Participating?
 
NetworkArchitekConnect With a Mentor Commented:
No, do this.

Create a static variable

private static myLock = new Object();


Then where it is potentially dangerous do:

lock(myLock)
{

// code

}
0
 
joshuadavidleeAuthor Commented:
is there soemthing i could write in the static class that would save me having to write anything in the thread classes
0
 
NetworkArchitekCommented:
Use the lock keyword. You could have the database class use the lock. That's one way to do it.
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
joshuadavidleeAuthor Commented:
code snippet please?
0
 
joshuadavidleeAuthor Commented:
for example how do i say

lock(this)

when its a static class and "this" is not allowed
0
 
joshuadavidleeAuthor Commented:
yes i just found that solution on msdn thanks !!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.