Because you must check the cache each time you get the item from it, you'd want to encapsulate this in a method that the constructor (called when the Web Service class is instantiated) and your methods can call to get the object from the cache:
publicclass MyWebService : WebService
// Initialize to boost performance of first call.
string content = GetContent();
if (content != null)
// do something with the content of the file that's cached.
string content = (string)Context.Cache[CacheName];
if (content == null)
string path = Server.MapPath("/path/to/virtual/file.txt");
using (StreamReader reader = new StreamReader(path))
content = reader.ReadToEnd();
privateconststring CacheName = "CONTENT";
This places the content of a file in the cache. The CacheDependency makes sure that if the file is changed, the cache item is invalidated. This means that null would be returned when trying to get the item from the cache. In this case, you read-in the content of that file and re-add it to the cache.
Your question is not clear as you use "each instance" and yet you seem to want to have a global value. If what you are trying to accomplish is to save yourself from having to re-read a semi-static file, here is what I would try...
// THE RESULTANT DATA
object myData = null;
// THE NAME OF THE FILE
string myFileName = @Server.MapPath(@"\dir\filename");
// GET THE INFO
System.IO.FileInfo fileInfo = new System.IO.FileInfo(myFileName);
// DOES IT EXIST
// THE NAME OF THE DATA KEY
string myDataKey = "DATA";
// THE NAME OF THE TIMESTAMP KEY
string myTimestampKey = "DATETIME";
// GET THE PREVIOUS TIME STAMP (IF ANY, ELSE A NULL)
DateTime myTimestamp = Application[myTimestampKey] as DateTime;
// GET THE TIMESTAMP
DateTime currentTimestamp = fileInfo.LastWriteTime;
// GET THE PREVIOUS DATA (IF ANY, ELSE A NULL)
myData = Application[myKey];
// ANY PREVIOUS DATA?
if (myData != null)
// DO WE HAVE A TIMESTAMP?
if (myTimestamp == null)
// SHOULD NOT HAPPEN, BUT JUST IN CASE (DATA AND NO TIMESTAMP)
myData = null;
// FILE CHANGED, RE-READ
myData = null;
// ALREADY THERE?
if (myData == null)
// GET THE DATA
myData = ...;
// AND SAVE FOR NEXT TIME
Application[myDataKey] = myData;
// AND SAVE THE TIMESTAMP
Application[myTimestampKey] = currentTimestamp;
Sorry for the confusion. What I want is a global value. I think I understand your example (thank you), however, I'm not sure where to put it in my code. The only place I can see to add it would be in the constructor, but I'm not sure if this would cause this code to be run each time the service received a request (I think it would).
If I understand everything correctly, then if I put your example in the constructor, then the first time the service handled a request (or when the file was changed) it would get the data, other wise is would skip the step to load the data. Is that right?
There is no member of the ListView class that specifies an item height, nor is there any Windows message or notification message (for custom drawing) that supports such an operation (for which you'd normally override WndProc in a child class and handle Windows messages the "old fashioned" way).
If you need this type of control, I suggest you take a look at the very robust third-party controls from companies like Developer Express[^] and Infragistics[^].
What I want to do is have a context menu come up whenever the user right-clicks a tab label.
Right now I added a context menu to my TabControl, and the context menu pops up regardless where the user right clicks, be it the tabpage or the tab label.
Is it possible for me to have the menu pop up only when the user right clicks the tab label (where the tab's name is displayed)?
Also, how can I deduce which tabpage was right clicked?
What I want is similar to the tabs in VS .Net. You right click a tab, it becomes the selected tab, and a little menu is displayed. And this only occurs when you click the tab name. This is the type of functionality I am looking for.
You could iterate through your TabControl.TabPages collection property and use the TabControl.GetTabRect to get the bounds of each tab (till you find a match). Use Rectangle.Contains (calling it on the Rectangle returned from GetTabRect to see if the mouse coordinates in the MouseDown event handler are over the specified tab. This will give you the tab that was clicked. You can then use ContextMenu.Show to show a ContextMenu at those coordinates. Show also tracks the ContextMenu so it continues to display until clicked.
Why, when so many solutions already exist? You don't need to assembly the frames in C# to be used in an ASP.NET web application. For example, some software will create MPEGs from frames and provide you with a URL that you can use with an embedded player (like Windows Media Player, QuickTime, RealPlayer , etc.).
If you insist on rewriting what's already been done a thousand times, you can assembly these frames in an AVI - an uncompressed (traditionally) video format. There is an article about this here on CodeProject: http://www.codeproject.com/csharp/steganodotnet4.asp[^].
If you want to encode this as something else, I recommend you look at Managed DirectX 9.0b (http://msdn.microsoft.com/directx[^]), which lets you easily enough pass the AVI through compressors. There are other ways, too, but again this type of software has already been written countless times.
Yes. It is the single instance of an object. Because of this reason, make sure you lock any shared resources in your implementation using the lock statement (compiles to use a Monitor) or a similar locking mechanism (mutexes, etc.), which you can find in the System.Threading namespace.
I've designed a form with a left and right section divided by a splitter component. in the left i've inserted a treeview component and on the right a panel with edits showing the data of the selected node in the tree.
I've seen that if i select a node and then focus on an edit control, in the tree the selection disappears.
Is there a property of a workaround to having always visible the selected item? In MFC when you use splitters, the splitted areas are standalone windows with their own focus handling.
There is a reason why they are called ListView and TreeView!
You could come up with something like this, but you would have to draw the ListView control yourself. You can look at this[^] article by Jon Rista, here on CodeProject, for a start or just use his control...