Solved

Static class accessed from many threads of different types

Posted on 2006-10-30
6
230 Views
Last Modified: 2010-04-16
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?
0
Comment
Question by:joshuadavidlee
  • 4
  • 2
6 Comments
 

Author Comment

by:joshuadavidlee
ID: 17839035
is there soemthing i could write in the static class that would save me having to write anything in the thread classes
0
 
LVL 10

Expert Comment

by:NetworkArchitek
ID: 17839075
Use the lock keyword. You could have the database class use the lock. That's one way to do it.
0
 

Author Comment

by:joshuadavidlee
ID: 17839081
code snippet please?
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:joshuadavidlee
ID: 17839104
for example how do i say

lock(this)

when its a static class and "this" is not allowed
0
 
LVL 10

Accepted Solution

by:
NetworkArchitek earned 500 total points
ID: 17839213
No, do this.

Create a static variable

private static myLock = new Object();


Then where it is potentially dangerous do:

lock(myLock)
{

// code

}
0
 

Author Comment

by:joshuadavidlee
ID: 17839220
yes i just found that solution on msdn thanks !!
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now